![]() |
Table of Contents |
![]() |
Script HostsScripts start out life as simple text files. Try this: Open Windows Notepad on a Windows XP computer, and type the following text: Set SNSet = GetObject("winmgmts:").InstancesOf ("Win32_OperatingSystem") for each SN in SNSet MMsgBox "The serial number for the installed OS is: " & SN.SerialNumber next Save the file as "SampleScript.vbs." Be sure to include the filename in double quotation marks, or Notepad will append a TXT filename extension. Now, locate the file in Windows Explorer. Make sure it has a VBS filename extension and double-click it. Provided you're running Windows XP and VBScript hasn't been disabled on your computer, you should see a small dialog box containing the serial number of your operating system. Congratulations, you've just scripted! NOTE For the time being, you don't need to worry about how this script does what it does. In later chapters, I'll explain what each of these four lines of code accomplishes. If you just can't wait, jump to Chapters 17 through 19, where I demonstrate how to use Windows Management Instrumentation to retrieve serial numbers and other operating system information. What actually happens when you double-click the VBS file? You can find out easily enough. From any Windows Explorer window, select Folder Options from the Tools menu. Select the File Types tab and locate VBS in the list. As shown in Figure 1.1, the VBS filename extension is associated with the Microsoft Windows Based Script Host. Whenever you double-click a VBS file, Windows fires up the Script Host, tells it which script you double-clicked, and lets the Script Host run the script. It's similar to what happens when you double-click a DOC file: Windows fires up Microsoft Word, tells it which file to open, and your document appears. Figure 1.1. File association for the VBS file typeSo, what is the Microsoft Windows Based Script Host (WSH)? It's a built-in component of Windows 2000, Windows XP, and Windows Server 2003. In fact, it's under Windows File Protection for those operating systems, meaning you can't delete or remove the WSH executable, Wscript.exe. WSH is also included with Windows Me, is an optional installation component in Windows 98, and can be added to Windows NT 4.0 and Windows 95 through a free download from www.Microsoft.com/scripting. TIP The latest version of WSH is 5.6, and you can download it for free from www.Microsoft.com/scripting. WSH is packaged in a Windows Installer file (MSI), so you can easily deploy it to your client computers via Windows Group Policy. WSH isn't included with some versions of Windows. In fact, WSH is present in many Microsoft products, in various versions. Here's where you can find WSH, along with the default versions:
NOTE For the purposes of this book, I'll always assume that you're running v5.6 of WSH. If you aren't, you can upgrade for free by downloading the newest version from www.Microsoft.com/scripting. WSH is simply a Windows application that reads scripts and executes them. Interestingly, VBScript is not implemented right within WScript.exe itself. WSH is actually intended to be extensible, and it supports a number of scripting languages besides VBScript. WSH does have a number of built-in functions, which is why it's nice to have the latest version-newer versions of more built-in functions. WSH can, for example, map network drives, connect to printers, work with environment variables, and modify the registry-all useful things to be able to do from within an administrative script. NOTE Other applications-such as Internet Explorer, Exchange Server, SQL Server, and IIS' Active Server Pages-can serve as script hosts, too. The nice part about learning to create Windows administration scripts in VBScript is that you can quickly learn to create SQL scripts, Exchange scripts, or even Active Server Pages, all using the same scripting language. |
![]() |
Table of Contents |
![]() |