Freigeben über


VBScript Overview

Microsoft® Windows® 2000 Scripting Guide

Microsoft® Visual Basic® Scripting Edition (VBScript) is often dismissed as being "just" a scripting language, the implication being that a scripting language is of little use to a system administrator faced with managing hundreds or even thousands of computers in an enterprise setting. Yet nothing could be further from the truth; when used in combination with technologies such as Windows Script Host (WSH), Windows Management Instrumentation (WMI), and Active Directory Service Interfaces (ADSI), VBScript becomes a powerful language for creating system administration tools. For example, using VBScript in combination with WMI and ADSI, you can write a script of 10,000 or so lines, complete with error handling, subroutines, and other advanced programming constructs. That single script can give you complete control over many aspects of your computing environment.

But what makes VBScript such a useful tool for system administrators is that you do not have to create such elaborate and complicated solutions. You can instead spend a few minutes typing a handful of lines of code into Notepad, and instantly create a custom solution to a particular problem.

For example, the three-line script in Listing 2.1 tells you how much free disk space is available on drive C of your computer.

Listing 2.1 Retrieving Free Disk Space Using VBScript

  
1
2
3
Set objWMIService = GetObject("winmgmts:")
Set objLogicalDisk = objWMIService.Get("Win32_LogicalDisk.DeviceID='c:'")
Wscript.Echo objLogicalDisk.FreeSpace

If you have been having problems with users filling up drive C on their computers, you now have a custom solution for identifying the computers running low on disk space, a solution developed using nothing more sophisticated than Notepad.

Of course, it might be that this script does not fully address your needs. For example, the script tells you the free space available only on your local computer; it cannot tell you how much free space is available on a remote computer. Likewise, the script reports the free space available only on drive C; it tells you nothing about free space available on drives D or E.

But if the script does not fully meet your needs, it can easily be modified, and without starting from scratch. This is another advantage of scripting in general and VBScript in particular: You can start with a very simple script and add to it as your needs change and as you become more proficient with the language. This chapter illustrates this process. It begins with the script shown in Listing 2.1, which reports the amount of free disk space on drive C. Subsequent sections in the chapter will take this simple three-line script and gradually add functionality to make it more useful in more situations. When this series of enhancements is complete, you will have a script that can:

  • Retrieve free disk space information for any computer in your organization, including remote computers.

  • Retrieve free disk space information from multiple computers.

  • Retrieve free disk space information for all drives installed in a computer.

  • Issue a notification only if a drive is low on disk space.

  • Continue to function if a user types an invalid computer name, or if a computer is not available over the network.

As new features are added to the script, the VBScript constructs required to add this functionality are briefly explained. After the script has been completed, a reference section will cover these constructs (and others) in more detail.