Skip to content

Instantly share code, notes, and snippets.

@milnak
Created March 18, 2025 18:21
Show Gist options
  • Save milnak/22b5749575b0abf4cc89520d26d34398 to your computer and use it in GitHub Desktop.
Save milnak/22b5749575b0abf4cc89520d26d34398 to your computer and use it in GitHub Desktop.
SysInfo - Displays system info using WMI
'
' File: SysInfo.vbs
' Created: May 29, 2002
' Version: 1.1
'
' Function: Displays system information using WMI
' Author: Jeff Miller
'
' History:
' 1.0 May 29, 2002 - initial revision
' 1.1 Sep 25, 2002 - rewrite, added additional properties
'
OPTION EXPLICIT
' ON ERROR RESUME NEXT
Dim objService
If VerifyHostIsCscript = False then
MsgBox "Please re-run this script using ""CScript SysInfo.vbs""", _
0, _
"This script requires CScript"
WScript.Quit
End If
If Not wmiConnect( "root\cimv2", objService ) Then
Wscript.Echo "Unable to connect to WMI root\cimv2 namespace"
Wscript.Echo "Please check the server name, credentials and WBEM Core."
WScript.Quit
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ShowInstancesOf "Computer Information", _
"Win32_ComputerSystem", _
"Name,PrimaryOwnerName,Domain,UserName,Manufacturer,Model,NumberOfProcessors,#TotalPhysicalMemory"
ShowInstancesOf "Processor Information", _
"Win32_Processor", _
"Name,Manufacturer,Description,CurrentClockSpeed,MaxClockSpeed,AddressWidth,ExtClock,L2CacheSize,L2CacheSpeed"
ShowInstancesOf "BIOS Information", _
"Win32_BIOS", _
"Manufacturer,Name,Version,@ReleaseDate,PrimaryBIOS,SMBIOSBIOSVersion,Status"
ShowInstancesOf "O/S Information", _
"Win32_OperatingSystem", _
"Caption,Version,BuildNumber,BuildType,BootDevice,SystemDevice,WindowsDirectory,SystemDirectory,CSName,CsdVersion,CurrentTimeZone,@InstallDate,@LastBootUpTime,Organization,OsLanguage,Primary,RegisteredUser,SerialNumber,#TotalVirtualMemorySize"
ShowInstancesOf "Disk Drive Information", _
"Win32_DiskDrive", _
"Manufacturer,Model,Description,InterfaceType,#Size,Partitions,ScsiBus,ScsiTargetID,DeviceID"
ShowInstancesOf "Logical Disk Information", _
"Win32_LogicalDisk", _
"Name,Description,VolumeName,FileSystem,#Size,#FreeSpace,VolumeSerialNumber"
ShowInstancesOf "Video Controller Information", _
"Win32_VideoController", _
"Description,CurrentHorizontalResolution,CurrentVerticalResolution,CurrentNumberOfColors,DeviceID,VideoProcessor,#AdapterRAM,InstalledDisplayDrivers,DriverVersion"
ShowInstancesOf "Printers", _
"Win32_Printer", _
"DeviceID,PortName,Default,Comment,Location,VerticalResolution,HorizontalResolution"
' I'd like to show ones only with IPEnabled=True, and iterate over IPAddress
' ShowInstancesOf "Network Adapter Configuration", _
' "Win32_NetworkAdapterConfiguration", _
' "Description,DHCPEnabled,@DHCPLeaseObtained,@DHCPLeaseExpires,DHCPServer,DNSHostname,DNSDomain,IPEnabled,MACAddress"
ShowNetworkAdapterConfiguration
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function wmiConnect(ByVal strNameSpace, ByRef objService)
' see "Setting the Default Process Security Level Using VBScript"
Const wbemImpersonationLevelImpersonate = 3
Dim objLocator, objWshNet
wmiConnect = True
'
' Create Locator object to connect to remote CIM object manager
'
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
If Err.Number then
Wscript.Echo "Error 0x" & CStr(Hex(Err.Number)) & _
" occurred in creating a locator object."
If Err.Description <> "" Then
Wscript.Echo "Error description: " & Err.Description & "."
End If
Err.Clear
wmiConnect = False
Exit Function
End If
'
' Connect to the namespace
'
Set objService = objLocator.ConnectServer( , strNameSpace )
ObjService.Security_.impersonationlevel = wbemImpersonationLevelImpersonate
If Err.Number then
Wscript.Echo "Error 0x" & CStr(Hex(Err.Number)) & _
" occurred in connecting to WMI"
If Err.Description <> "" Then
Wscript.Echo "Error description: " & Err.Description & "."
End If
Err.Clear
wmiConnect = False
End If
objLocator = null
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function FormatString(strProperty)
If IsNull(strProperty) then
FormatString = "n/a"
Else
FormatString = strProperty
End If
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function WmiDateToString(strDate)
' WMI dates are in the format: 20020925111300.000000-420
If IsNull( strDate ) Then
WmiDateToString = "n/a"
Else
WmiDateToString = Mid( strDate, 5, 2 ) & "/" & _
Mid( strDate, 7, 2 ) & "/" & _
Mid( strDate, 1, 4 )
End if
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function FormatNum( num )
If IsNull( num ) then
FormatNum = "n/a"
exit function
end if
if num > 1073741823 then
FormatNum = FormatNumber( num/1073741824, 0 ) & " GB"
elseif num > 1048575 then
FormatNum = FormatNumber( num/1048576, 0 ) & " MB"
elseif num > 1023 then
FormatNum = FormatNumber( num/1024, 0 ) & " KB"
else
FormatNum = FormatNumber( num, 0 ) & " bytes"
end if
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function VerifyHostIsCscript()
Dim strFullName
strFullName = WScript.FullName
If Err.Number = 0 Then
Dim i
Dim j
i = InStr(1, strFullName, ".exe", 1)
If i <> 0 Then
j = InStrRev(strFullName, "\", i, 1)
If j <> 0 And LCase( Mid(strFullName, j+1, i-j-1) ) = "cscript" Then
VerifyHostIsCScript = True
Exit Function
End If
End If
End If
VerifyHostIsCScript = false
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function FormatPropName( propName )
Const PropWidth = 25
FormatPropName = Left( propName & ":" & String( PropWidth, " " ), propWidth )
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub ShowInstancesOf( caption, wmiClass, propertyPaths )
' propertyPaths uses form "path,#path,@Date"
' where # means format as number, and @ means to format as date
Const wbemFlagReturnImmediately = 16
Const wbemFlagForwardOnly = 32
Const wbemImpersonationLevelImpersonate = 3
Dim objSet, obj
Dim paths
paths = Split( propertyPaths, "," )
WScript.Echo caption
WScript.Echo String( Len(caption), "-" )
Set objSet = objService.InstancesOf( wmiClass, _
wbemFlagReturnImmediately+wbemFlagForwardOnly )
For Each obj in objSet
Dim i
Dim pathName
' See MSDN for "SWbemPropertySet.Item"
For i = LBound(paths) To UBound(paths)
If Left( paths(i), 1 ) = "#" Then
' format as number
pathName = Mid( paths(i), 2 )
WScript.Echo FormatPropName( pathName ) & _
FormatNum( obj.Properties_.Item( pathName ) )
ElseIf Left( paths(i), 1 ) = "@" Then
' format as date
pathName = Mid( paths(i), 2 )
WScript.Echo FormatPropName( pathName ) & _
WmiDateToString( obj.Properties_.Item( pathName ) )
Else
' format as string (default)
pathName = paths(i)
WScript.Echo FormatPropName( pathName ) & _
FormatString( obj.Properties_.Item( pathName ) )
End If
Next
WScript.Echo ""
Next
objSet = null
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub ShowNetworkAdapterConfiguration( )
Dim objSet, obj
Dim i
WScript.Echo "Network Adapter Configuration"
WSCript.Echo "-----------------------------"
Set objSet = objService.ExecQuery( "SELECT Caption,Description,IPAddress,MACAddress,DNSDomain,DHCPEnabled,DHCPServer " & _
"FROM Win32_NetworkAdapterConfiguration " & _
"WHERE IPEnabled = TRUE" )
For Each obj In objSet
If Not IsNull( obj.IPAddress ) Then
WScript.Echo FormatPropName("Description") & FormatString( obj.Description )
WScript.Echo FormatPropName("MACAddress") & FormatString( obj.MACAddress )
WScript.Echo FormatPropName("DNSDomain") & FormatString( obj.DNSDomain )
WScript.Echo FormatPropName("DHCP") & obj.DHCPEnabled & " - " & _
FormatString( obj.DHCPServer )
For i = LBound( obj.IPAddress ) To UBound( obj.IPAddress )
WScript.Echo FormatPropName("IPAddress") & obj.IPAddress(i)
Next
End If
WScript.Echo ""
Next
objSet = null
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment