Home > Articles > Programming > Visual Basic

  • Print
  • + Share This
This chapter is from the book

Testing the Script

If you jumped ahead and already tried to execute the final script, you realize that it's flawed. If you haven't, go ahead and give it a whirl now. Take a few minutes to see if you can track down the problem. There are actually three errors, and here are some hints.

  • One is a simple typo.

  • One is a sort of logic error, where something isn't being used properly for the situation.

  • The last one is a typo, and could have been avoided if I had followed my own advice from earlier in the book.

Can you find them all? The first one is an easy mistake: I simply forgot a closing parentheses.

'connect to domain and retrieve
'a list of member objects
Dim oDomain
Set oDomain = GetObject("WinNT://" & sDomain
   

The correct code should be Set oDomain = GetObject("WinNT://" & sDomain). The next one's a bit trickier.

'open an output file
Dim oOutput
oOutput = oFSO.CreateTextFile("\\server1\public\output.txt")

Can you see it? I'm using oOutput to represent an object, but I forgot to use the Set keyword when making the assignment. VBScript requires Set whenever you're assigning an object to a variable. The corrected code looks like this.

'open an output file
Dim oOutput
Set oOutput = oFSO.CreateTextFile("\\server1\public\output.txt")
   

The last error is tricky, too. It's in the GetOSInfo() function.

Function GetOSInfo(sComputer)

      'declare variables
      Dim objWMIService
      Dim colItems
      Dim strOutput

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

Did you find it? The problem is that I used the wizard-generated code, which uses "str" as a prefix for string variables. I'm in the habit of using the shorter prefix "s" for string variables, and that's where my problem lies. In the function definition, I declared sComputer, but in the line of code that connects to the WMI service, I used strComputer. I continued using sComputer elsewhere, so strComputer is wrong. Here's the corrected code snippet.

Function GetOSInfo(sComputer)

      'declare variables
      Dim objWMIService
      Dim colItems
      Dim strOutput

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

The problem with this error is that it doesn't cause a problem for the script; the script will execute just fine. You just won't get any results, because the script would try to connect to a computer named "". I mentioned that I could have avoided this problem by following my own advice. Had I included Option Explicit, VBScript would have produced an error on the offending line of code, because strComputer wasn't declared. sComputer, on the other hand, is implicitly declared because it's part of a function declaration. You'll notice that I did the same thing with strOutput and sOutput, meaning they'll have to be corrected, too.

Just to make sure you've got it all, Listing 20.5 includes the complete, corrected script. Remember that this script is also available on the CD that accompanies this book.

Listing 20.5. InventoryDomain2.vbs. This corrected script produces the expected results.

'get domain name
Dim sDomain
sDomain = InputBox("Enter domain to inventory")

'connect to domain and retrieve
'a list of member objects
Dim oDomain
Set oDomain = GetObject("WinNT://" & sDomain)

'get the filesystemobject
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

'open an output file
Dim oOutput
Set oOutput = oFSO.CreateTextFile("\\server1\public\output.txt")

'run through the objects
Dim oObject, sComputerName, sDetails
For Each oObject In oDomain

 'is this object a computer?
 If oObject.Class = "Computer" Then

  'yes - get computer name
  sComputerName = oObject.Name

  'get OS info
  sDetails = GetOSInfo(sComputerName)

  'write info to the file
  oOutput.Write sDetails

 End If
Next
'close the output file
oOutput.Close

'release objects
Set oOutput = Nothing
Set oFSO = Nothing
Set oObject = nothing
Set oDomain = Nothing

'display completion message
WScript.Echo "Output saved to \\server1\public\output.txt"

Function GetOSInfo(sComputer)

      'declare variables
      Dim objWMIService
      Dim colItems
      Dim strOutput

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

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

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

      '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

Testing a large script like this is much easier with the Script Debugger. You can spot lines that are causing trouble just by following the execution path.

For more information on the Script Debugger, see "Testing the Script" in Chapter 13. You can also read up on the Script Debugger in the VBScript documentation at http://msdn.microsoft.com/scripting.

  • + Share This
  • 🔖 Save To Your Account