VB WMI Examples

August 23rd, 2009 Leave a comment Go to comments

All below examples are based on establishing a connection to a remote computer and performing a WMI query.

Computer Disk Information

Retrieves information about all physical disks (distinguished by the numerical value 3), and displays ins volume identifier (drive letter) and free vs used space.

Const HARD_DISK = 3

Set objWMIService = GetObject(“winmgmts:” & “{impersonationLevel=impersonate}!\\” & “COMPUTER” & “\root\cimv2″)

Set colDisks = objWMIService.ExecQuery (“Select * from Win32_LogicalDisk Where DriveType = ” & HARD_DISK & “”)
For Each objDisk in colDisks
‘objDisk.FreeSpace
‘Wscript.Echo objDisk.DeviceID
Wscript.Echo objDisk.DeviceID + ” Disk Size (GB): ” + CStr(Round((((objDisk.Size / 1024)/1024)/1024),1))
Wscript.Echo objDisk.DeviceID + ” Free Disk Space (GB): ” + CStr(Round((((objDisk.FreeSpace / 1024)/1024)/1024),2))
Next

Set colDisks = objWMIService.ExecQuery (“Select * from Win32_LogicalDisk Where DriveType = ” & HARD_DISK & “”)

For Each objDisk in colDisks

Wscript.Echo objDisk.DeviceID + ” Disk Size (GB): ” + CStr(Round((((objDisk.Size / 1024)/1024)/1024),1))

Wscript.Echo objDisk.DeviceID + ” Free Disk Space (GB): ” + CStr(Round((((objDisk.FreeSpace / 1024)/1024)/1024),2))

Next

Network Card Information

Retrieves information about the Network Cards configured on the computer, looping through all valid adapters present.

Set objWMIService = GetObject(“winmgmts:” & “{impersonationLevel=impersonate}!\\” & “COMPUTER” & “\root\cimv2″)

Set colAdapters = objWMIService.ExecQuery (“Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE”)

j = 1

For Each objAdapter in colAdapters

Wscript.Echo “===Adapter ” & CStr(j)

If Not IsNull(objAdapter.IPAddress) Then

For i=LBound(objAdapter.IPAddress) to UBound(objAdapter.IPAddress)

Wscript.Echo “IP Address: ” + objAdapter.IPAddress(i)

WScript.Echo “Subnet: ” & objAdapter.IPSubnet(i)

Next

WScript.Echo “Gateway: ” & objAdapter.DefaultIPGateway(0)

For i = 0 To UBound(objAdapter.DNSServerSearchOrder)

WScript.Echo “DNS Server: ” & objAdapter.DNSServerSearchOrder(i)

Next

For i = 0 To UBound(objAdapter.DNSDomainSuffixSearchOrder)

WScript.Echo “DNS suffix list: ” & objAdapter.DNSDomainSuffixSearchOrder(i)

Next

End If

j = j+1

Next

Operating System / up-time Information

Retrieves Information about the host operating system and current service pack, also information on the up-time of the host; be aware that if the regional settings differ the results may appear inaccurate.

Set colOperatingSystems = objWMIService.ExecQuery (“Select * from Win32_OperatingSystem”)

For Each objOS in colOperatingSystems

dtmBootup = objOS.LastBootUpTime

dtmLastBootupTime = WMIDateStringToDate(dtmBootup)

dtmSystemUptime = DateDiff(“n”, dtmLastBootUpTime, Now)

Wscript.Echo “Uptime: ” & CStr(Round((dtmSystemUptime /60) /24)) & ” Day(s)”

Next

For Each objOperatingSystem in colOperatingSystems

Wscript.Echo “OS: ” & objOperatingSystem.Caption & ” ” & objOperatingSystem.Version

Wscript.Echo “Service Pack: ” & objOperatingSystem.ServicePackMajorVersion & “.” & objOperatingSystem.ServicePackMinorVersion

Next

Function WMIDateStringToDate(dtmBootup)

WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & “/” & _

Mid(dtmBootup, 7, 2) & “/” & Left(dtmBootup, 4) _

& ” ” & Mid (dtmBootup, 9, 2) & “:” & _

Mid(dtmBootup, 11, 2) & “:” & Mid(dtmBootup,13, 2))

End Function

Symantec Antivirus Definition Version

This query has been tested on versions 7.x and 9.x. The principle is to connect to the remote file system and read the contents of the file named “definfo.dat” to define the date / version of the definitions loaded on the target computer.

Set objFSO = CreateObject(“Scripting.FileSystemObject”)

Set objFile = objFSO.GetFile(“\\” & “COMPUTER” & “\c$\Program Files\Common Files\Symantec Shared\VirusDefs\definfo.dat”)

If objFSO.FileExists(objFile) Then

Set objDatFile = objFSO.OpenTextFile(objFile, 1)

Do Until objDatFile.AtEndOfStream

strLine = objDatFile.Readline

intCurDefs = InStr(strLine , “CurDefs”)

If intCurDefs > 0 Then

strCurDefs = strLine

strDateDefs = Mid(strCurDefs, 9, 8 )

dtYear = Left(strDateDefs, 4)

dtMonth = Mid(strDateDefs, 5, 2)

dtDay = Right(strDateDefs, 2)

DateVirDefs = dtMonth & “/” & dtDay & “/” & dtYear

dtDefDate = CDate(DatevirDefs)

strRevNumber = Right(strCurDefs, 3)

Wscript.Echo “Definition: ” & dtDefDate & ” ” & strRevNumber

End If

Loop

objDatFile.Close

Else

Wscript.Echo “The file definfo.dat does not exist”

End If

All of the above queries have been rolled up with into once script which also combines an AD query element; this script can be downloaded here

  1. No comments yet.
  1. No trackbacks yet.