Archive

Posts Tagged ‘VB’

VB WMI Examples

August 23rd, 2009 No 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

Categories: Uncategorized Tags: , ,

VB WMI Query Computer Attributes

August 2nd, 2009 No comments

By using a VB scripts we can perform a WMI (Windows Management Instrumentation) connection to a host to execute a query against.

Below is an example of a WMI query connecting to the host named “computer”, once connected will loop around each drive letter which is a hard drive displaying the drive letter and total disk space in GB

On Error Resume Next
Const HARD_DISK = 3

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

if Err.Number = 0 then
‘ Add WMI Query Script in here!

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))

Next

else
Wscript.echo “Could not connect to ” & “computer” & ” error ” & Err.Number
End If

Err.Clear

The above WMI query can be combined with an Active Directory query to make the script more versatile (further information is here), an example of this script is below:

On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
Const HARD_DISK = 3

Set objConnection = CreateObject(“ADODB.Connection”)
Set objCommand =   CreateObject(“ADODB.Command”)
objConnection.Provider = “ADsDSOObject”
objConnection.Open “Active Directory Provider”

Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = “Select Name from ‘LDAP://OU=MyServers,DC=Domain,DC=co,DC=uk’ ” & “Where objectClass=’computer’”
objCommand.Properties(”Page Size”) = 1000
objCommand.Properties(”Searchscope”) = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
Wscript.Echo “Computer Name: ” & objRecordSet.Fields(“Name”).Value

Set objWMIService = GetObject(“winmgmts:” & “{impersonationLevel=impersonate}!\\” & objRecordSet.Fields(“Name”).Value & “\root\cimv2″)

if Err.Number = 0 then

‘ Add WMI Query Script in here!

else
Wscript.echo “Could not connect to ” & objRecordSet.Fields(“Name”).Value & ” error ” & Err.Number
End If

Err.Clear
objRecordSet.MoveNext
Loop

A number of WMI queries used can be found in the reference page WMI_Queries

VB Query Active Directory Objects

June 13th, 2009 No comments

In an Active Directory environment there is often a requirement to query objects within the AD  database, either returning selected attribute values or using this information to for further queries.

The same principles apply across any object type, the script below connects via LDAP, performs a query and returns selected values from Active Directory.

The below script will query all objects in the OU MyServers and subtree, in the domain Domain.co.uk, of type computer, returning the attributes Name and Location.

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject(”ADODB.Connection”)
Set objCommand = CreateObject(”ADODB.Command”)
objConnection.Provider = “ADsDSOObject”
objConnection.Open “Active Directory Provider”

Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = “Select Name, Location from ‘LDAP://OU=MyServers,DC=Domain,DC=co,DC=uk’ ” & “Where objectClass=’computer’”
objCommand.Properties(”Page Size”) = 1000
objCommand.Properties(”Searchscope”) = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF

‘ Insert Operators here
objRecordSet.MoveNext

Loop

Once the query has been run the results can then be used for any requirement, a simple example below is to display  the name of the computer followed by the Location attribute stored in AD.

……
Do Until objRecordSet.EOF

‘ Insert Operators here
Wscript.Echo “Computer Name: ” & objRecordSet.Fields(”Name”).Value
Wscript.Echo “Location: ” & objRecordSet.Fields(”Location”).Value

objRecordSet.MoveNext
Loop

Any attribute of the object can be displayed as long as it was requested in the initial query; Select Name, Location from…. , attribute names are LDAP attributes rather than field names seen in the Active Directory Users and Computers mmc. A list of attributes can be found here; Active Directory LDAP Attributes

One way to enhance this script further is for each computer selected perform a WMI query against it; VB WMI Query