Previous Section Table of Contents Next Section

Writing the Query in VBScript

If you're like me, you like your final scripts to be clean, consistent, and easy to read. Using the Wizard- or Scriptomatic-generated scripts isn't the best way to achieve consistency. For example, the PrimalScript Wizard always includes the following code.


On Error Resume Next

Dim strComputer

Dim objWMIService

Dim colItems



strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

First, you might not want error-checking turned off, which is what On Error Resume Next does. You might use a different variable naming convention (I often do, mainly because I'm a bit too lazy to type str instead of just s for string variables and the like), or you might have already defined a variable name that the wizard is using. Understand that you can always revise and modify the template scripts to fit better within your overall scripts. Not only can you change them, you probably should change them.

Suppose you want to write a script that restarts a remote server. You've done your browsing, and Win32_OperatingSystem has a method named Shutdown that looks like it'll do the trick. Using Scriptomatic or the WMI Query Wizard, you generate code similar to the following.


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

First, you weren't interested in querying and displaying any information, so you can start by wiping out all of the WScript.Echo lines, leaving you with the following.


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

Next

Regardless of how many operating systems the computer thinks it has, the one that's running is the one you want to shut down, and that'll be the primary one. You can modify the WQL query to just retrieve that instance of the class.


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 WHERE " & _

 "Primary = TRUE",,48)

For Each objItem in colItems

Next

You probably don't want to shut down just the local computer, so you'll want to add some kind of prompt that collects the appropriate computer name.


On Error Resume Next

Dim strComputer

Dim objWMIService

Dim colItems



strComputer = InputBox("Shut down what computer?")

Set objWMIService = GetObject("winmgmts:\\" & _

 strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery( _

 "Select * from Win32_OperatingSystem WHERE " & _

 "Primary = TRUE",,48)

For Each objItem in colItems

Next

Now, let's say you want to use different variable names, and you don't want to turn off error checking. No problem-just be sure you change the variable names every time they appear in the script. A search and replace function is the most reliable way to do so, and you'll wind up with something like this.


Dim sComputer

Dim oWMIService

Dim oItems, oItem



sComputer = InputBox("Shut down what computer?")

Set oWMIService = GetObject("winmgmts:\\" & _

 sComputer & "\root\cimv2")

Set oItems = oWMIService.ExecQuery( _

 "Select * from Win32_OperatingSystem WHERE " & _

 "Primary = TRUE",,48)

For Each oItem in oItems

Next

Now, you need to add the actual Shutdown method.


Dim sComputer

Dim oWMIService

Dim oItems, oItem



sComputer = InputBox("Shut down what computer?")

Set oWMIService = GetObject("winmgmts:\\" & _

 sComputer & "\root\cimv2")

Set oItems = oWMIService.ExecQuery( _

 "Select * from Win32_OperatingSystem WHERE " & _

 "Primary = TRUE",,48)

For Each oItem in oItems

 oItem.ShutDown()

Next

There, you've customized the template script to meet your exact needs. Really, you're not using much of the original wizard-generated code: You kept the variable declarations, the basic WQL query, and the For Each…Next construct. That's about it.

NOTE

It may seem odd to use a For Each…Next construct when you know your modified query will only return one instance. Why bother? Because the ExecQuery method will always return a collection, even if the query only returns one instance into the collection. You could have eliminated the For Each…Next construct and used oItems(0).Shutdown() instead, using the oItems(0) syntax to reference the first (and to your knowledge, the only) instance in the collection. Either way works fine.


    Previous Section Table of Contents Next Section