(view source code of srvinv.vbs as plain text)
' Use custom error handling (in this case: none at all)
On Error Resume Next
' Define constants
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
' Check "named" command line arguments (command line switches)
If WScript.Arguments.Named.Exists( "?" ) Then
Syntax
End If
If WScript.Arguments.Named.Exists( "H" ) Then
CreateNewLogs
End If
If WScript.Arguments.Named.Exists( "L" ) Then
boolLog = True
Else
boolLog = False
End If
If WScript.Arguments.Named.Exists( "S" ) Then
boolSoftInv = True
Else
boolSoftInv = False
End If
If WScript.Arguments.Named.Exists( "Q" ) Then
boolQuiet = True
Else
boolQuiet = False
End If
If boolQuiet And Not boolLog Then
Syntax
End If
' Check "unnamed" command line parameters
Select Case WScript.Arguments.Unnamed.Count
Case 0
' Default if none specified is local computer (".")
Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
' Retrieve computer name
Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
For Each objItem in colItems
strComputer = objItem.Name
Next
Case 1
' Command line parameter can either be a computer
' name or "/?" to request online help
strComputer = UCase( Wscript.Arguments.Unnamed(0) )
If InStr( strComputer, "?" ) > 0 Then
Syntax
End If
Case Else
' Maximum is 1 "unnamed" command line parameter
Syntax
End Select
' Initialize variables
strLog = strComputer
strMsg = vbCrLf & "Computer name : " & strComputer & vbCrLf
' Connect to the specified (or default) computer
Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/CIMV2" )
' Retrieve processor info
Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_Processor", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly )
For Each objItem In colItems
strCPUType = Strip( objItem.Name )
Next
' Retrieve system info
Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_ComputerSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly )
For Each objItem In colItems
If objItem.Domain <> "" Then
strLog = strLog & vbTab & objItem.Domain
strMsg = strMsg & "Domain : " & objItem.Domain & vbCrLf
Else
strLog = strLog & vbTab & objItem.Workgroup
strMsg = strMsg & "Domain : " & objItem.Workgroup & vbCrLf
End If
strLog = strLog & vbTab & objItem.Manufacturer
strMsg = strMsg & "Manufacturer : " & objItem.Manufacturer & vbCrLf
strLog = strLog & vbTab & objItem.Model
strMsg = strMsg & "Model : " & objItem.Model & vbCrLf
strLog = strLog & vbTab & objItem.NumberOfProcessors
strMsg = strMsg & "Processors : " & objItem.NumberOfProcessors & vbCrLf
strLog = strLog & vbTab & strCPUType
strMsg = strMsg & "Processor type : " & strCPUType & vbCrLf
strRoles = ""
For Each strItem in objItem.Roles
strTemp = strItem
For Each strTest in "LM_Workstation,LM_Server,NT,Server_NT,Backup_Browser,Potential_Browser,"
If strItem = strTest Then
strTemp = ""
End If
Next
If strTemp <> "" Then
If strRoles = "" Then
strRoles = strRoles & ", " & strTemp
Else
strRoles = strTemp
End If
End If
Next
' strRoles = Join( objItem.Roles, ", " )
strLog = strLog & vbTab & strRoles
strMsg = strMsg & "Roles : " & strRoles & vbCrLf
strMemory = Int( ( objItem.TotalPhysicalMemory / 1048576 ) + 0.5 )
strLog = strLog & vbTab & strMemory
strMsg = strMsg & "Memory : " & strMemory & " MB" & vbCrLf
Next
' Retrieve OS info
Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_OperatingSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly )
For Each objItem In colItems
strLog = strLog & vbTab & objItem.Caption
strMsg = strMsg & "OS Version : " & objItem.Caption & vbCrLf
strLog = strLog & vbTab & objItem.ServicePackMajorVersion & "." & objItem.ServicePackMinorVersion
strMsg = strMsg & "SP Version : " & objItem.ServicePackMajorVersion & "." & objItem.ServicePackMinorVersion & vbCrLf
strLog = strLog & vbTab & objItem.SerialNumber
strMsg = strMsg & "Serial Number : " & objItem.SerialNumber & vbCrLf
Next
' Retrieve IP addresses
strIPAddress = ""
strPrevious = ""
strMsgIP = ""
strLogIP = ""
Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly )
For Each objItem In colItems
strIPAddress = Join( objItem.IPAddress, "," )
If strIPAddress <> strPrevious Then
If strLogIP = "" Then
strLogIP = strIPAddress
strMsgIP = "IP Address : " & strIPAddress
Else
strLogIP = strLogIP & ", " & strIPAddress
strMsgIP = "IP Addresses : " & strLogIP
End If
strPrevious = strIPAddress
End If
Next
strMsg = strMsg & strMsgIP & vbCrLf
strLog = strLog & vbTab & strLogIP
' Retrieve disk info
Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_LogicalDisk WHERE MediaType = 12", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly )
strLogDisk = ""
strMsgDisk = ""
For Each objItem In colItems
strDisk = objItem.DeviceID & " " & Int( ( objItem.Size / 1073741824 ) + 0.5 )
strFree = objItem.DeviceID & " " & Int( ( 100 * objItem.FreeSpace / objItem.Size ) + 0.5 )
If strLogDisk = "" Then
strLogDisk = strDisk
strMsgDisk = strDisk & " GB"
strLogFree = strFree
strMsgFree = strFree & " %"
Else
strLogDisk = strLogDisk & ", " & strDisk
strMsgDisk = strMsgDisk & ", " & strDisk & " GB"
strLogFree = strLogFree & ", " & strFree
strMsgFree = strMsgFree & ", " & strFree & " %"
End If
Next
strMsg = strMsg & "Logical Drives : " & strMsgDisk & vbCrLf
strLog = strLog & vbTab & strLogDisk
strMsg = strMsg & "Free Space : " & strMsgFree & vbCrLf
strLog = strLog & vbTab & strLogFree
' Display result unless Quiet mode was specified (/Q switch)
If Not boolQuiet Then
WScript.Echo strMsg
End If
' Perform software inventory if specified (/S switch)
If boolSoftInv Then
strLogSoft = ListSoftware( )
If boolLog Then
Set fso = CreateObject( "Scripting.FileSystemObject" )
Set filOut = fso.OpenTextFile( "softinv.csv", ForAppending, True )
filOut.WriteLine( strLogSoft )
filOut.Close
set fso = Nothing
End If
End If
'Append the server inventory to a CSV file if Logging was specified (/L switch)
If boolLog Then
Set fso = CreateObject( "Scripting.FileSystemObject" )
Set filOut = fso.OpenTextFile( "srvinv.csv", ForAppending, True )
filOut.WriteLine( strLog )
filOut.Close
End If
' End of main program
WScript.Quit( 0 )
Sub CreateNewLogs( )
' Header line for server inventory log
strLog = "Server" & vbTab _
& "Domain" & vbTab _
& "Manufacturer" & vbTab _
& "Model" & vbTab _
& "#CPUs" & vbTab _
& "CPU type" & vbTab _
& "Server roles" & vbTab _
& "Memory (MB)" & vbTab _
& "Windows version" & vbTab _
& "SP" & vbTab _
& "Windows serial number" & vbTab _
& "IP address" & vbTab _
& "Partition size (GB)" & vbTab _
& "Free space"
Set fso = CreateObject( "Scripting.FileSystemObject" )
Set filOut = fso.OpenTextFile( "srvinv.csv", ForWriting, True )
filOut.WriteLine( strLog )
filOut.Close
' Header line for software inventory log
strLog = "Server" & vbTab _
& "Software" & vbTab _
& "Version"
' Set fso = CreateObject( "Scripting.FileSystemObject" )
Set filOut = fso.OpenTextFile( "softinv.csv", ForWriting, True )
filOut.WriteLine( strLog )
filOut.Close
set fso = Nothing
' Abort script
WScript.Quit( 0 )
End Sub
Function ListSoftware( )
On Error Resume Next
strLogSoft = ""
strMsgSoft = vbCrLf & "Installed software:" & vbCrLf
strPrevious = ""
Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/CIMV2" )
Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_ProductSoftwareFeatures", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly )
For Each objItem In colItems
arrProdInfo = Split( objItem.Product, "," )
If strPrevious <> arrProdInfo(1) Then
arrProdName = Split( arrProdInfo(1), "=" )
strProdName = arrProdName(1)
If Left( strProdName, 1 ) = Chr(34) Then
strProdName = Mid( strProdName, 2 )
End If
If Right( strProdName, 1 ) = Chr(34) Then
strProdName = Mid( strProdName, 1, Len( strProdName ) - 1 )
End If
arrProdVer = Split( arrProdInfo(2), "=" )
strProdVer = arrProdVer(1)
If Left( strProdVer, 1 ) = Chr(34) Then
strProdVer = Mid( strProdVer, 2 )
End If
If Right( strProdVer, 1 ) = Chr(34) Then
strProdVer = Mid( strProdVer, 1, Len( strProdVer ) - 1 )
End If
strMsgSoft = strMsgSoft & strProdName & ", Version " & strProdVer & vbCrLf
strLogSoft = strLogSoft & strComputer & vbTab & strProdName & vbTab & strProdVer & vbCrLf
End If
strPrevious = arrProdInfo(1)
Next
If Not boolQuiet Then
WScript.Echo strMsgSoft
End If
ListSoftware = strLogSoft
End Function
Sub ShowError( )
strMsg = vbCrLf & "Reference # " & strRef & vbCrLf & "Error # " _
& Err.Number & vbCrLf & Err.Description & vbCrLf & vbCrLf
Syntax
End Sub
Private Function Strip( strInput )
Do While Left( strInput, 1 ) = " "
strInput = Mid( strInput, 2 )
Loop
Strip = strInput
End Function
Sub Syntax( )
strMsg = strMsg & vbCrLf _
& "SrvInv.vbs, Version 1.21" & vbCrLf _
& "Display and log server inventory" & vbCrLf & vbCrLf _
& "Usage: [ CSCRIPT ] SRVINV.VBS [ server_name ]" _
& " [ /H ] [ /L ] [ /Q ] [ /S ]" & vbCrLf & vbCrLf _
& "Where: " & Chr(34) & "server_name" & Chr(34) _
& " is the optional name of the server to be probed" & vbCrLf _
& " (default is local computer " _
& "name)" & vbCrLf _
& " /H create new csv file(s) with Header" & vbCrLf _
& " /L Log results in csv file(s)" & vbCrLf _
& " /Q Quiet mode, no screen display (requires /L)" & vbCrLf _
& " /S perform Software inventory too" & vbCrLf & vbCrLf _
& "Written for Vincent Segers" & vbCrLf _
& "by Rob van der Woude" & vbCrLf _
& "http://www.robvanderwoude.com" & vbCrLf & vbCrLf _
& "Created with Microsoft's Scriptomatic 2.0 tool" & vbCrLf _
& "http://www.microsoft.com/downloads/details.aspx?" & vbCrLf _
& " FamilyID=09dfc342-648b-4119-b7eb-783b0f7d1178&DisplayLang=en" & vbCrLf
WScript.Echo strMsg
WScript.Quit(1)
End Sub
page last modified: 2024-04-16; loaded in 0.0098 seconds