Putting It All Together: Your First WMI/ADSI Script
IN THIS CHAPTER
It's time to leverage what you've learned about ADSI and WMI scripting. In this chapter, I'll walk you through the entire design and creation process for a new script. In addition to demonstrating a useful new purpose for WMI and ADSI, this chapter will help strengthen your script design skills.
By now, you should have a good idea of what WMI and ADSI can do for you. In this chapter, I'll walk you through the complete design process for an entirely new script. This time, I'll use both WMI and ADSI in the same script. The script's job will be to check in on every computer in an Active Directory or NT domain and query some information about its operating systems. I want the script to output this information to a text file on a file server. The information I want to collect includes operating system version, service pack level, number of processors in the machine, maximum physical memory in the machine, and so forth. This is a useful way to quickly inventory a network and see what machines might need to be upgraded before deploying a new application, or to see what machines don't have the latest service pack applied.
Designing the Script
My script is a reasonably complex undertaking, so it helps to break it down into manageable tasks. I need the script to do three things:
Query a list of computers from the domain.
Query information from each computer.
Write information out to a text file.
The last bit is probably the easiest. I can use the FileSystemObject to open a text file, write information to it, and then close the text file. Something like the following would work.
Dim oFSO, oFile Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFile = oFSO.CreateTextFile("output.txt") oFile.Write "Information" oFile.Close
For more information on using the FileSystemObject, refer to Chapter 12.
Querying a list of computers from the domain shouldn't be too hard, either. If I want the script to work with both NT and Active Directory domains, I need to use the WinNT ADSI provider, because only that provider works with both domains. I can query all of the objects in the domain, and then use an If…Then construct to work with only the computer objects. Code such as the following should do the trick.
Dim oDomain Set oDomain = GetObject("WinNT://" & sDomain) Dim oObject, sComputerName, sDetails For Each oObject In oDomain 'is this object a computer? If oObject.Class = "Computer" Then 'yes do something with it End If Next
For more information on querying domains by using ADSI, see Chapter 14, and see "Querying Domain Information" in Chapter 15.
Pulling the operating system (OS) information is tougher. WMI seems like the way to go, but WMI has about three gazillion classes. Which one do I need? Fortunately, I have a way to cheat. My primary script editor is Sapien Technology's PrimalScript 3.0, and it includes a WMI Script Wizard.
A trial version of PrimalScript 3.0 is included on the CD that accompanies this book.
Running the wizard displays the dialog box shown in Figure 20.1. The left side of the dialog box shows a list of every WMI class that my computer knows about. Scrolling through the list, I find that there's a class named Win32_OperatingSystem. That seems like a good place to start.
Figure 20.1. The WMI Wizard starts with a list of all available WMI classes.
Clicking the Win32_OperatingSystem class changes the dialog box to look like the one shown in Figure 20.2. Here, the wizard has filled in a sample script capable of querying information from the selected class. I see things like service pack level and operating system version, so this is probably the class I want. The wizard offers an Insert button to immediately insert this code into my script, and a Copy button to copy the code to the clipboard. Listing 20.1 shows the complete wizard code.
Figure 20.2. The wizard generates sample code to query the selected class.
I've added line breaks and line continuation characters (_) to Listing 20.1 so that it will fit in this book.
Listing 20.1. WizardCode.vbs. This code queries the Win32_OperatingSystem class and outputs all of the class' attributes and their values.
On Error Resume Next Dim strComputer Dim objWMIService Dim colItems strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & _ strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery( _ "Select * from Win32_OperatingSystem",,48) For Each objItem in colItems WScript.Echo "BootDevice: " & objItem.BootDevice WScript.Echo "BuildNumber: " & objItem.BuildNumber WScript.Echo "BuildType: " & objItem.BuildType WScript.Echo "Caption: " & objItem.Caption WScript.Echo "CodeSet: " & objItem.CodeSet WScript.Echo "CountryCode: " & objItem.CountryCode WScript.Echo "CreationClassName: " & objItem.CreationClassName WScript.Echo "CSCreationClassName: " & _ objItem.CSCreationClassName WScript.Echo "CSDVersion: " & objItem.CSDVersion WScript.Echo "CSName: " & objItem.CSName WScript.Echo "CurrentTimeZone: " & objItem.CurrentTimeZone WScript.Echo "Debug: " & objItem.Debug WScript.Echo "Description: " & objItem.Description WScript.Echo "Distributed: " & objItem.Distributed WScript.Echo "EncryptionLevel: " & objItem.EncryptionLevel WScript.Echo "ForegroundApplicationBoost: " & _ objItem.ForegroundApplicationBoost WScript.Echo "FreePhysicalMemory: " & _ objItem.FreePhysicalMemory WScript.Echo "FreeSpaceInPagingFiles: " & _ objItem.FreeSpaceInPagingFiles WScript.Echo "FreeVirtualMemory: " & objItem.FreeVirtualMemory WScript.Echo "InstallDate: " & objItem.InstallDate WScript.Echo "LargeSystemCache: " & objItem.LargeSystemCache WScript.Echo "LastBootUpTime: " & objItem.LastBootUpTime WScript.Echo "LocalDateTime: " & objItem.LocalDateTime WScript.Echo "Locale: " & objItem.Locale WScript.Echo "Manufacturer: " & objItem.Manufacturer WScript.Echo "MaxNumberOfProcesses: " & objItem.MaxNumberOfProcesses WScript.Echo "MaxProcessMemorySize: " & objItem.MaxProcessMemorySize WScript.Echo "Name: " & objItem.Name WScript.Echo "NumberOfLicensedUsers: " & objItem.NumberOfLicensedUsers WScript.Echo "NumberOfProcesses: " & objItem.NumberOfProcesses WScript.Echo "NumberOfUsers: " & objItem.NumberOfUsers WScript.Echo "Organization: " & objItem.Organization WScript.Echo "OSLanguage: " & objItem.OSLanguage WScript.Echo "OSProductSuite: " & objItem.OSProductSuite WScript.Echo "OSType: " & objItem.OSType WScript.Echo "OtherTypeDescription: " & objItem.OtherTypeDescription WScript.Echo "PlusProductID: " & objItem.PlusProductID WScript.Echo "PlusVersionNumber: " & objItem.PlusVersionNumber WScript.Echo "Primary: " & objItem.Primary WScript.Echo "ProductType: " & objItem.ProductType WScript.Echo "QuantumLength: " & objItem.QuantumLength WScript.Echo "QuantumType: " & objItem.QuantumType WScript.Echo "RegisteredUser: " & objItem.RegisteredUser WScript.Echo "SerialNumber: " & objItem.SerialNumber WScript.Echo "ServicePackMajorVersion: " & _ objItem.ServicePackMajorVersion WScript.Echo "ServicePackMinorVersion: " & _ objItem.ServicePackMinorVersion WScript.Echo "SizeStoredInPagingFiles: " & _ objItem.SizeStoredInPagingFiles WScript.Echo "Status: " & objItem.Status WScript.Echo "SuiteMask: " & objItem.SuiteMask WScript.Echo "SystemDevice: " & objItem.SystemDevice WScript.Echo "SystemDirectory: " & objItem.SystemDirectory WScript.Echo "SystemDrive: " & objItem.SystemDrive WScript.Echo "TotalSwapSpaceSize: " & _ objItem.TotalSwapSpaceSize WScript.Echo "TotalVirtualMemorySize: " & _ objItem.TotalVirtualMemorySize WScript.Echo "TotalVisibleMemorySize: " & _ objItem.TotalVisibleMemorySize WScript.Echo "Version: " & objItem.Version WScript.Echo "WindowsDirectory: " & objItem.WindowsDirectory Next
The wizard's code pulls more information than I want, and it's displaying the information in message boxes, rather than writing them to a file, but the code makes a great place to start. I can easily modify it to meet my needs.
The script is designed! I identified the three major tasks that the script needs to be able to complete, and I've created some prototype code that can be adapted to the script's exact requirements. In short, I now know how to do everything I need; I just need to rearrange it and customize it.
What, No Wizard?
If you're not using PrimalScript, there are some other tools you can use to make WMI scripting easier. In Chapter 18, for example, I introduced Microsoft's Scriptomatic tool, which performs a similar function to the PrimalScript WMI Wizard. You can also dive into the WMI documentation in the MSDN Library (http://msdn.microsoft.com/library), which documents each WMI class and includes some scripting examples.