|
|
|
What’s a WSH?

by Greg Chapman, Senior Systems Engineer
This article originally appeared in All 'Bout Computers Ezine
A WSH isn’t something you do upon a star, it isn’t something you do behind your ears and it isn’t the sound my airplane makes as it goes over your house - that sounds more like an old Model T Ford running past you at head-height. It is probably something the Cap’n (Cap’n Patt. You know him. No parrot, still has his legs, doesn’t bite) will wind up putting in his GeekSpeak column sometime soon, though. And it is something you probably already have.
If you’ve ever had the Iloveyou or the AnnaKournikova worms advertise their presence through affectionate emails to your friends, you’ve probably got WSH.
WSH stands for the Windows Script Host and, despite how you feel about viruses, it’s a good thing. I should state now and without equivocation that while those worms would probably not have succeeded without WSH, the fault was more to do with Outlook and Social Engineering than the presence of a script processor. In fact, it’s because of the ease with which those viruses executed so flawlessly and quickly that you might consider giving a closer look at what this wonderful tool offers.
For many years, scripting languished as a hobby for ultra geeky Unix folks who wanted to manipulate their Shells (a shell is an operating environment. You can think of Explorer as a Shell. You can also consider the command prompt as a shell) or who wrote very portable, fast and powerful tools in a language called Perl. I hesitate to offer this idea but if you ever wrote batch files for use in DOS, well, you were doing some shell scripting there, too.
The problem in the Windows world, though, has always been the same one faced by the majority of Macintosh users; in a GUI based system, doing something more than once means lots of mouse clicks to repeat the same steps over and over. You need a macro language and a processor for that language in order to get those repetitive tasks done. And if you want to do that job without first opening Word or some other macro enabled application, you were out of luck on Windows systems.
Through nearly 6 years of whining about administering Windows NT systems manually, Microsoft introduced the Windows Script Host. Those who couldn’t wait for Microsoft to get off the stick for a scripting environment native to the operating system, there was Perl and those folks still run Perl code on their Windows systems to do those big, boring, repetitive tasks.
Perl is not really a language you would want to start your infant on. Neither is English, for that matter. But if you have started teaching the kids your first language, it’s likely they’ll be able to pick up VBScript. And, if you’re already writing HTML pages with Javascript and VBScript for the server side processing, you probably already know enough to put the Windows Script Host to work. Why? When you visit a web page that is authored with script functions in it, you’re really providing a place for that script to execute or be hosted (script host, get it?). That script host is your web browser. It knows the languages, how to deliver that script to the language processor and then how to show the results.
But if you want to use those languages outside your web browser…you’re pretty much out of luck. And that’s where WSH comes in. It provides a conduit to the script processor and a place for that processor to return visible results. At last, with this tool on board, you can write a text file containing VBScript or Javascript instructions and have them execute…even if you aren’t logged into your system at the time! WSH provides that environment. You can even install other languages like Perl and Python and have them execute within WSH, too!
But why would you do that? To understand what’s so handy about a script instead of an executable, first understand that an Executable like Microsoft Word is a collection of code that has been converted (compiled) to a binary condition that allows the code to execute on your system. That’s great for fast and reliable execution. But if there’s something you want that executable to do differently there’s no way you can change that executable file by yourself. You need a new executable provided to you by the programmer after she has modified it with your request, recompiled it and then sent it back to you. Microsoft, for one, doesn’t do that very often and they rarely do it for free.
A script, however, is generally a plain old text file which you point to a script processor. The script processor then compiles the code and executes it. If you need a change made here, you don’t need special tools for the job. Notepad is good enough because we’re working with text! If you learn the script language, you don’t even need someone else to provide you with the changes; you can make them yourself!
Here’s a simple way to tell if you have WSH – open a command prompt (that’s command.com for you Win9x users and CMD.EXE for the NT/2000/XP folks) and type the following at the prompt: CSCRIPT
Press enter. You should see something like this:
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Usage: CScript scriptname.extension [option...] [arguments...]
Options:
| //B |
Batch mode: Suppresses script errors and prompts from displaying |
| //D |
Enable Active Debugging |
| //E:engine |
Use engine for executing script |
| //H:CScript |
Changes the default script host to CScript.exe |
| //H:WScript |
Changes the default script host to WScript.exe (default) |
| //I |
Interactive mode (default, opposite of //B) |
| //Job:xxxx |
Execute a WSF job |
| //Logo |
Display logo (default) |
| //Nologo |
Prevent logo display: No banner will be shown at execution time |
| //S |
Save current command line options for this user |
| //T:nn |
Time out in seconds: Maximum time a script is permitted to run |
| //X |
Execute script in debugger |
| //U |
Use Unicode for redirected I/O from the console |
CScript and its sister, WScript, are the two faces to the Windows Script Host that accept a file full of script and get it executed. WScript provides a light GUI interface and CScript provides a command line style interface. CScript is not the default processor WSH uses but you’ll want to use it the majority of the time.
Note that the first line contains a piece of information describing the version of WSH installed on your system. The current version is 5.6 and if you don’t have it or you got an error, you can get WSH by visiting the Downloads link at http://msdn.microsoft.com/scripting. While you’re there, check out the documentation and very helpful articles available to help get you started. In addition, the public newsgroups hosted by Microsoft at msnews.microsoft.com are full of people with questions and, more importantly, answers on how to use WSH, the languages and the rest of the script accessible tools exposed through WSH. Find out more by setting up a newsreader and subscribe to the newsgroup Microsoft.public.scripting.wsh which is hosted at news://msnews.microsoft.com. If you’re unfamiliar with newsreaders, you can also go to http://groups.google.com and take a look at them from this link.
Just for giggles, you can try this quick, illustrative example of a WSH compatible script written with VBScript as the language.
Open Notepad and copy the two lines of code below into a new, empty text file:
strYourName= _
InputBox(“Ya, hey dere! Type your name in the box, man!”)
wscript.echo _
“You’re kidding!!! I never woulda guessed your name was “ _
& strYourName
Save the file to your hard drive as Demo.vbs and make a note of the path to which you save it. Now, from a command prompt, type:
CScript <drive>:\<path>\demo.vbs
Where <drive>:\<path> is the drive and path to which you saved the file. Press enter and cooperate with your new smart-mouthed script!
So that’s what a WSH is!
|