Search code examples
vbscriptinventory-management

Manage server script


I tried to use PStool to write some code for further server managemnt, but how can i do it within a lot servers automatically? Here is my code to handle only one server:

    ' list reg key value
    psexec \\<<sever name>> -u <<userName>> -p <<password>> reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\CrashControl > <<server Name>>_regedit.txt
    ' list network information
    psexec \\<<sever name>> -u <<userName>> -p <<password>> ipconfig /all
    ' list services.msc
    psservice \\<<server name>> -u <<username>> -p <<password>> query > <<serverName>>_service.txt
    ' list the hotfix
    psinfo -h \\<<servername>> -u <<adminUsername>> -p <<adminPassword>> > <<serverName>>_hotfix.txt
    ' list the installed software
    psinfo -s \\<<servername>> -u <<adminUsername>> -p <<adminPassword>> > <<serverName>>_Software.txt

how can i add these to .vbs and execute a server list file (csv or txt) automatically?

Kindly help!


Solution

  • Instead of the PsTools you'd use WMI in VBScript. Basic example:

    server = "hostname"
    
    Set wmi = GetObject("winmgmts://" & server & "/root/cimv2")
    For Each obj In wmi.ExecQuery("SELECT * FROM wmi_class")
      WScript.Echo obj.property
    Next
    

    Replace hostname with the actual hostname, wmi_class with the name of a WMI class, and property with the name of an actual property of that class. Relevant classes for you are:

    In some cases you may need to restrict the results returned by the query with a WHERE clause:

    SELECT * FROM wmi_class WHERE property = 'value'
    

    See here and here for more information about WQL queries.

    You can also use WMI to work on the registry. In your particular scenario, you'll need the EnumValues method to enumerate the values of the registry key, and then query the values using the respective getter method:

    Const HKLM = &H80000002
    
    server  = "hostname"
    keyPath = "SYSTEM\CurrentControlSet\Control\CrashControl"
    
    Set reg = GetObject("winmgmts:{impersonationLevel=impersonate}!//" _ 
      & server & "/root/default:StdRegProv")
    reg.EnumValues HKLM, keyPath, valueNames, valueTypes
    For i = 0 To UBound(valueNames)
      Select Case valueTypes(i)
        Case REG_DWORD
          reg.GetDWORDValue HKLM, keyPath, valueNames(i), value
          WScript.Echo value
        Case ...
          ...
      End Select 
    Next
    

    Note that unlike before you're using the namespace /root/default:StdRegProv here.

    Reading the server names from a CSV can be done using the FileSystemObject methods. For a CSV with a structure like this:

    hostname,address,...
    host_A,10.23.42.1,...
    host_B,10.23.42.2,...
    ...
    

    you'd do something like this:

    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set f = fso.OpenTextFile("C:\path\to\your.csv")
    f.SkipLine  'skip header line
    Do Until f.AtEndOfStream
      server = Split(f.ReadLine)(0)
      ...
    Loop
    f.Close
    

    Note that if the CSV is in Unicode format, you'll have to change the line

    Set f = fso.OpenTextFile("C:\path\to\your.csv")
    

    into this:

    Set f = fso.OpenTextFile("C:\path\to\your.csv", 1, False, True)