Windows Tip Sheet

Remote Scripting with Windows Script Host

Conquer your world of servers from home.

Go “On Location” Without Getting Up
Are you one of those efficient administrators who insists on automating as many tasks as possible in your environment? Yeah, okay, me too. Maybe “lazy” is a better word than “efficient,” but it’s all the same to the boss, right? The hot tool for automation these days is scripting, whether you consider that to be writing VBScripts, command-line scripts, KiXtart, ScriptLogic or whatever. Writing scripts is definitely where it’s at for automated administration. One problem I’ve had recently, though, is a number of VBScript-based scripts I’ve written that need to be run locally on several different servers at about the same time.

Remember: “Lazy”
I’m well aware of the fact that I could tromp into the datacenter, load the script on each server, and run it. But some of the servers are sitting in datacenters on the other side of the planet, and I can’t afford enough shoe leather to tromp that far. Yeah, I know — Remote Desktop. Well, it so happens that some of these are NT 4.0 servers and some of the others don’t have Remote Desktop (or Terminal Services Remote Admin Mode) enabled.

All is not lost, of course. Windows Script Host (WSH), the bit that runs VBScripts, is capable of remote scripting. At least, the latest version (5.6) is capable of it, and that version is available for everything from Windows 95 and later, including my venerable NT file servers. WSH’s remote scripting capability can copy a script from my local machine to each of the remote servers, execute the script on the remote servers — where it runs locally — and then report back and tell me how everything went.

Suppose my script was named “c:\maint.vbs,” and that I needed to run it on a list of computers I’d entered into a text file named “c:\computers.txt,” listing one server name per line. Assuming that I’ve already deployed WSH 5.6 to all of these machines, which is easy enough, it’s packages in an MSI that SMS (or Group Policy) can push right out. Here’s the script I’d run on my local workstation to deploy the thing:

Dim oFSO, oTSin, oController, oRemote, sComputer
Set oController = CreateObject(“WSHController”)
Set oFSO = CreateObject(“Scripting.FileSystemObject”)
Set oTSin = oFSO.OpenTextFile(“c:\computers.txt”)

Do Until oTSin.AtEndOfStream
 sComputer = oTSin.ReadLine
 Set oRemote = oController.CreateScript(“c:\maint.vbs”, _
  sComputer)
 oRemote.Execute

 Do While oRemote.Status = 0
  WScript.Sleep 1000
 Loop

 WScript.Echo “Finished script on “ & sComputer

Loop
oTSin.Close
WScript.Echo “Finished all computers”

The WSHController object controls the copying of remote scripts. The CreateScript() method actually copies the script and returns an object — oRemote, in my example here — which represents the remote script. I then use the oRemote object’s Execute() method to run the remote script and check its Status property to see if it’s finished or not.

Remote scripting with WSHController might not solve every “lazy day” problem, but it’s definitely a cool way to push scripts out to multiple computers (automatically, with the script I’m using here) and run it.

Micro-Tips

Did you know that you can copy GPOs from one domain to another? GPOs are stored as binary files on domain controllers, but the internals of the file aren’t tied to a specific domain or anything. Just figure out the GPO’s Globally Unique Identifier (GUID) in the source domain; you’ll find the GPO files in a folder named after the GUID, within a DC’s NETLOGON share. Create a new GPO in the target domain, and copy the contents of the GPO into that folder (which will have a different GUID name). Let file replication take place and your GPO is copied.

Do you know how time sync works in an AD domain? It’s critical to the way Kerberos works, so be sure you do. Clients sync with the DC that authenticated them. DCs sync to their domain’s PDC Emulator. PDC emulators sync to their parent domain’s PDC Emulator, up to the forest root PDC Emulator, which should be configured to sync with an authoritative time source, like the U.S. Naval Observatory’s Atomic Clock or your Aunt Sarah’s old grandfather clock, whichever is easier.

More Resources
• My scripting-related Web site at http://www.scriptinganswers.com
• OnScript, a script editor with basic built-in remote scripting capabilities (lets you store and execute scripts remotely from within the editor’s UI): http://www.onscript.com
• Download WSH 5.6 for Win2000 and later: http://www.microsoft.com/downloads/details.aspx?familyid=C717D943-7E4B-4622-86EB-95A22B832CAA&displaylang=en
• Download WSH 5.6 for Win9x and NT: http://www.microsoft.com/downloads/details.aspx?familyid=0A8A18F6-249C-4A72-BFCF-FC6AF26DC390&displaylang=en

About the Author

With more than fifteen years of IT experience, Don Jones is one of the world’s leading experts on the Microsoft business technology platform. He’s the author of more than 35 books, including Windows PowerShell: TFM, Windows Administrator’s Scripting Toolkit, VBScript WMI and ADSI Unleashed, PHP-Nuke Garage, Special Edition Using Commerce Server 2002, Definitive Guide to SQL Server Performance Optimization, and many more. Don is a top-rated and in-demand speaker and serves on the advisory board for TechMentor. He is an accomplished IT journalist with features and monthly columns in Microsoft TechNet Magazine, Redmond Magazine, and on Web sites such as TechTarget and MCPMag.com. Don is also a multiple-year recipient of Microsoft’s prestigious Most Valuable Professional (MVP) Award, and is the Editor-in-Chief for Realtime Publishers.

comments powered by Disqus

Reader Comments:

Tue, Aug 28, 2007 Anonymous Anonymous

The double quotes get transformed into characters that look similar, but notepad doesn't recognize them as double quotes. Thanks, the code helped me immensly.

Wed, Aug 8, 2007 Anonymous Anonymous

>How can I run a command of command prompt or .bat file in a script ?

by googling moar

good article

Fri, Jan 26, 2007 Iny Anonymous

How can I run a command of command prompt or .bat file in a script ?

Tue, Jan 23, 2007 Iny Anonymous

Ups, I find the problem. In registry key is necessary to switch from 0 to 1 value "Remote" Thank you.

Tue, Jan 23, 2007 Iny Anonymous

I have the same problem. I want to execute script on many computers in the domain where I am a domain administrator. Version of WSH is 5.6.0.6626 (c:program files,system32,wscript.exe). I I correct the " characters but I still cannot run script remote because it give me an error "ActiveX component can't create object , line 8, char2, - 800A01AD" This line start with "Set oRemote = oCon..." My script maint.vbs is OK. I don't know where is problem. I'm desperate with this. I really need this. can you help me ?

Thu, Jun 8, 2006 John Norway

Brnedan,
If you copied this off the web page into notepad it copies invalid characters. open your file in Textpad and you will see that the " characters are wrong.

Tue, Jan 31, 2006 Brendan Hawaii

This script seems fine, but I am getting an invalid character error at the beginning quotation mark of line 2, right before WSHController. Can anyone help?

Thu, Oct 13, 2005 rhone Anonymous

Hi!
I have tested this script but I have the following message: An Active Composant cannot create one object
Hiw can I do for executing correctely this script.

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.