Previous Section Table of Contents Next Section

Writing the Main Script

The function was probably the toughest part to write; with that out of the way, I can adapt my prototype code to create the main script, shown in Listing 20.3.

Listing 20.3. MainScript.vbs. Queries the domain, creates the output file, and calls the custom function I already wrote.

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"

I'll provide my usual walk-through of this script in a bit; for now, try to pick out the adapted pieces of prototype code. Notice where I'm querying the domain, opening and writing to the text file, closing the text file, and calling the GetOSInfo() function.

graphics/arrow.gif Inventorying the Domain

Listing 20.4 shows the complete, ready-to-run script. Get this ready to run, but don't execute it just yet. In the next section, I'll cover testing and troubleshooting this script.

Listing 20.4. InventoryDomain.vbs. The complete domain inventory script.

'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

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:\\" & _

       strComputer & "\root\cimv2")



      'get item collection

      Set colItems = objWMIService.ExecQuery( _

       "Select * from Win32_OperatingSystem",,48)



      'init output string

      sOutput = String(70,"-")

      sOutput = sOutput & 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: " & _

      Next             objItem.WindowsDirectory & vbCrLf



 'return results

      GetOSInfo = sOutput



End Function

You need to change where this script puts its output file before using it in your environment. The script prompts for the domain name, so you won't have to make any changes there.

graphics/arrow.gif Inventorying the Domain-Explained

The script starts by prompting for the domain name. This allows the script to be used in a multidomain environment. The domain name is stored in a string variable.


'get domain name

Dim sDomain

sDomain = InputBox("Enter domain to inventory")

Next, the script uses ADSI to connect to the domain and retrieve a list of all domain objects. This may be a lengthy operation in a large domain, because computer, user, and all other objects are included in the results.


'connect to domain and retrieve

'a list of member objects

Dim oDomain

Set oDomain = GetObject("WinNT://" & sDomain

The script creates a new FileSystemObject and assigns it to a variable.


'get the filesystemobject

Dim oFSO

Set oFSO = CreateObject("Scripting.FileSystemObject")

The script now creates a new text file by using the FileSystemObject's CreateTextFile method. The method returns a TextStream object, which is assigned to the variable oOutput.


'open an output file

Dim oOutput

oOutput = oFSO.CreateTextFile("\\server1\public\output.txt")

oDomain now represents all of the objects in the domain; I'll use a For Each…Next loop to iterate through each object in turn. Within the loop, oObject will represent the current object.


'run through the objects

Dim oObject, sComputerName, sDetails

For Each oObject In oDomain

Because oDomain contains more than just computers, I need to check each object to see if its Class property equals "Computer." That way, I can just work with the computer objects and skip the rest.


 'is this object a computer?

 If oObject.Class = "Computer" Then

For objects that are a computer, I pull the computer name into a variable. Then, I assign the results of GetOSInfo() to variable sDetails. Finally, I write sDetails to the output text file using the TextStream object's Write method. Closing up the loop with Next moves on to the next object in the domain.


  'yes - get computer name

  sComputerName = oObject.Name



  'get OS info

  sDetails = GetOSInfo(sComputerName)



  'write info to the file

  oOutput.Write sDetails



 End If

Next

When I'm done with all the objects, I close the output file, release all the objects I created by setting them equal to Nothing, and then display a simple completion message.


'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"

Here's that function I wrote earlier. It starts with basic variable declaration.


Function GetOSInfo(sComputer)



      'declare variables

      Dim objWMIService

      Dim colItems

      Dim strOutput

Next is pure wizard code, which uses GetObject to connect to the specified computer's WMI service.


'get WMI service

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

 strComputer & "\root\cimv2")

After I am connected, I execute a query to retrieve the Win32_OperatingSystem class.


'get item collection

Set colItems = objWMIService.ExecQuery( _

 "Select * from Win32_OperatingSystem",,48)

I set up my output string to include a line of hyphens and the current computer name.


'init output string

sOutput = String(70,"-")

sOutput = sOutput & sComputer

Finally, I append the WMI information to the output string.


'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

With the main script finished, I return the output string as the function's result.


 'return results

      GetOSInfo = sOutput



End Function

There you have it-a nice, easy-to-use administrative script that uses both WMI and ADSI to accomplish a useful task.

    Previous Section Table of Contents Next Section