Home > Articles > Home & Office Computing > Microsoft Windows Desktop

This chapter is from the book

Writing Functions and Subroutines

The one bit of functionality that seems to be standalone is the code generated by the wizard, which will do my WMI querying for me. I might need to use that code in another script someday, and I'll definitely be using it over and over in the script I'm writing now, so it makes sense to write it as a function.

I want the function to accept a computer name, query that computer for specific operating system information, and then compile all that information into a neatly formatted string. The function should return the string to the main script, which can then write it to a file or whatever.

Adapting the wizard's code isn't too difficult. Listing 20.2 shows my new GetOSInfo() function. Note that this isn't intended to be run as a standalone script; as a function, it must be called by another script, which must provide the name of the computer to connect to as the function's input parameter.

Listing 20.2. GetOSInfo.vbs. This function queries a computer's operating system information and returns the results in a string.

Function GetOSInfo(sComputer)

      'declare variables
      Dim objWMIService
      Dim colItems
      Dim strOutput

      'get WMI service
      Set objWMIService = GetObject("winmgmts:\\" & _
       strComputer & "\root\cimv2")

      'get item collection
      Set colItems = objWMIService.ExecQuery( _
       "Select * from Win32_OperatingSystem",,48)

      'init output string
      sOutput = String(70,"-") & vbCrLf
      sOutput = sOutput & sComputer & vbCrLf

      'append info to output string
      For Each objItem in colItems
strOutput = strOutput & "BuildNumber: " & _
             objItem.BuildNumber & vbCrLf
            strOutput = strOutput & "BuildType: " & _
             objItem.BuildType & vbCrLf
            strOutput = strOutput & "Caption: " & _
             objItem.Caption & vbCrLf
            strOutput = strOutput & "EncryptionLevel: " & _
             objItem.EncryptionLevel & vbCrLf
            strOutput = strOutput & "InstallDate: " & _
             objItem.InstallDate & vbCrLf
            strOutput = strOutput & "Manufacturer: " & _
             objItem.Manufacturer & vbCrLf
            strOutput = strOutput & "MaxNumberOfProcesses: " & _
             objItem.MaxNumberOfProcesses & vbCrLf
      strOutput = strOutput & "MaxProcessMemorySize: " & _
             objItem.MaxProcessMemorySize & vbCrLf
       strOutput = strOutput & "Name: " & _
             objItem.Name & vbCrLf
            strOutput = strOutput & _
             "NumberOfLicensedUsers: " & _
             objItem.NumberOfLicensedUsers & vbCrLf
            strOutput = strOutput & "NumberOfProcesses: " & _
             objItem.NumberOfProcesses & vbCrLf
            strOutput = strOutput & "NumberOfUsers: " & _
             objItem.NumberOfUsers & vbCrLf
            strOutput = strOutput & "OSProductSuite: " & _
             objItem.OSProductSuite & vbCrLf
            strOutput = strOutput & "OSType: " & _
             objItem.OSType & vbCrLf
            strOutput = strOutput & "OtherTypeDescription: " & _
             objItem.OtherTypeDescription & vbCrLf
            strOutput = strOutput & "Primary: " & _
             objItem.Primary & vbCrLf
            strOutput = strOutput & "ProductType: " & _
             objItem.ProductType & vbCrLf
            strOutput = strOutput & "RegisteredUser: " & _
             objItem.RegisteredUser & vbCrLf
            strOutput = strOutput & "SerialNumber: " & _
             objItem.SerialNumber & vbCrLf
            strOutput = strOutput & _
             "ServicePackMajorVersion: " & _
             objItem.ServicePackMajorVersion & vbCrLf
            strOutput = strOutput & _
             "ServicePackMinorVersion: " & _
             objItem.ServicePackMinorVersion & vbCrLf
            strOutput = strOutput & "Version: " & _
             objItem.Version & vbCrLf
            strOutput = strOutput & "WindowsDirectory: " & _
             objItem.WindowsDirectory & vbCrLf
       Next

  'return results
       GetOSInfo = sOutput

End Function

I didn't have to do much to adapt the script. First, I deleted all the lines that I didn't want in my script. I changed all the WScript.Echo commands to strOutput = strOutput &, which appends the information into a string rather than displays it in a message box. I also added & vbCrLf to the end of each line, which adds a carriage return and linefeed character. Those help keep the final output file looking nice.

I also dressed up the code at the beginning of the function.

     'declare variables
     Dim objWMIService
     Dim colItems
     Dim strOutput

     'get WMI service
     Set objWMIService = GetObject("winmgmts:\\" & _
      strComputer & "\root\cimv2")

     'get item collection
     Set colItems = objWMIService.ExecQuery( _
      "Select * from Win32_OperatingSystem",,48)

     'init output string
     sOutput = String(70,"-") & vbCrLf
     sOutput = sOutput & sComputer & vbCrLf

I added some comments to document the code—PrimalScript isn't so good about that—and I initialized my sOutput variable. I also started sOutput off to contain a line of 70 hyphens, and the name of the computer I'm querying. These extra touches help make the final output file easier to read and more useful.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.