Vbscript WMI Gather information from all Domain computers to MDB


Hi,

This is a little extensive, but in the end I think you will enjoy reading.

What script does:

Gather all information in all computers and servers in a certain domain. This can be useful if you pretend to upgrade, or plan infra-structure changes, or just create your own CMDB.

We will gather information about:

hardware, software, printers, network, OS, Firewall, Disks, Shares, Users, event viewer, etc..

You need to run this with an administrative account in domain.

You need to have a database (Microsoft access in this example script)

DIM fuser, WshNetwork SET WshNetwork = WScript.CreateObject("WScript.Network") fUser = WshNetwork.UserName DIM objConnectionMDB, objRecordsetMDB, objWMIService Set objConnectionMDB = CreateObject("ADODB.Connection") Set objRecordset = CreateObject("ADODB.Recordset") objConnectionMDB.Provider="Microsoft.Jet.OLEDB.4.0" objConnectionMDB.Open "\\path\log\log.mdb" ' type here the location of your mdb file Const adUseClient = 3 Const adOpenDynamic = 2 Const adLockPessimistic = 2 'active directory connection DIM objRecordSetAD, objConnectionAD, objCommand Set objConnectionAD = CreateObject("ADODB.Connection") Set objCommand =   CreateObject("ADODB.Command") objConnectionAD.Provider = "ADsDSOObject" objConnectionAD.Open "Active Directory Provider" Const ADS_SCOPE_SUBTREE = 2

For each information type we will a different procedure.

Computer Information

sub computer(strComputer) on error resume next Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48) For Each objItem in colItems On Error Resume Next objRecordsetMDB.CursorLocation = adUseClient objRecordsetMDB.Open "SELECT * FROM computer" , objConnectionMDB ,adOpenDynamic, adLockPessimistic objRecordsetMDB.AddNew objRecordsetMDB("Computer") = strComputer objRecordsetMDB("OSTYPE") = objItem.CreationClassName objRecordsetMDB("Hardwareprovider") = objItem.Manufacturer objRecordsetMDB("model") = objItem.Model objRecordsetMDB.Update objRecordsetMDB.Close objRecordsetMDB = Nothing Next end sub

Software Information

sub software(strComputer) on error resume next Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colSoftware = objWMIService.ExecQuery ("Select * from Win32_Product") For Each objSoftware in colSoftware on error resume next objRecordsetMDB.CursorLocation = adUseClient objRecordsetMDB.Open "SELECT * FROM Software" , objConnectionMDB ,adOpenDynamic, adLockPessimistic objRecordsetMDB.AddNew objRecordsetMDB("computer") = strComputer objRecordsetMDB("Caption") = objSoftware.Caption objRecordsetMDB("Description") = objSoftware.Description objRecordsetMDB("id") = objSoftware.IdentifyingNumber objRecordsetMDB("InstallDate") = objSoftware.InstallDate2 objRecordsetMDB("Installlocation") = objSoftware.InstallLocation objRecordsetMDB("Installstate") =     objSoftware.InstallState objRecordsetMDB("name") = objSoftware.Name objRecordsetMDB("cache") = objSoftware.PackageCache objRecordsetMDB("sku") = objSoftware.SKUNumber objRecordsetMDB("vendor") = objSoftware.Vendor objRecordsetMDB("Version") = objSoftware.Version objRecordsetMDB.Update objRecordsetMDB.Close objRecordsetMDB = Nothing Next End sub

Nework Information

sub network(strComputer) on error resume next Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colAdapters = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") For Each objAdapter in colAdapters objRecordsetMDB.CursorLocation = adUseClient objRecordsetMDB.Open "SELECT * FROM SistemaRede" , objConnectionMDB ,adOpenStatic, adLockOptimistic objRecordsetMDB.AddNew objRecordsetMDB("computer") = strComputer objRecordsetMDB("Model") = objAdapter.Description objRecordsetMDB("ip") = objAdapter.IPAddress(i) objRecordsetMDB("dhcp") = objAdapter.DHCPEnabled objRecordsetMDB("dhcpserver") = objAdapter.DHCPServer objRecordsetMDB("mask") = objAdapter.IPSubnet(i) objRecordsetMDB("gateway") = objAdapter.DefaultIPGateway(i) objRecordsetMDB("gateway") = objAdapter.DefaultIPGateway objRecordsetMDB("mac") = objAdapter.MACAddress objRecordsetMDB("wins1") = objAdapter.WINSPrimaryServer objRecordsetMDB("wins2") = objAdapter.WINSSecondaryServer objRecordsetMDB("dnsname") = objAdapter.DNSDomain objRecordsetMDB("dns1") = objAdapter.DNSServerSearchOrder(1) objRecordsetMDB("dns2") = objAdapter.DNSServerSearchOrder(2) objRecordsetMDB("date") = date() objRecordsetMDB.Update objRecordsetMDB.Close objRecordsetMDB = Nothing Next end sub

Operating System Information

sub OS(strComputer) on error resume next Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime") Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem") For Each objOperatingSystem in colOperatingSystems dtmConvertedDate.Value = objOperatingSystem.InstallDate dtmInstallDate = dtmConvertedDate.GetVarDate objRecordsetMDB.CursorLocation = adUseClient objRecordsetMDB.Open "SELECT * FROM OperatingSystem" , objConnectionMDB ,adOpenStatic, adLockOptimistic objRecordsetMDB.AddNew objRecordsetMDB("computer") = strComputer objRecordsetMDB("description") = objOperatingSystem.Caption objRecordsetMDB("ServicePack") = objOperatingSystem.ServicePackMajorVersion objRecordsetMDB("gateway") = objOperatingSystem.Version objRecordsetMDB("dateInstall") = dtmInstallDate objRecordsetMDB("date") = date() objRecordsetMDB.Update objRecordsetMDB.Close objRecordsetMDB = Nothing Next end sub

Patching Information

sub patching(strComputer) on error resume next Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colQuickFixes = objWMIService.ExecQuery ("Select * from Win32_QuickFixEngineering") For Each objQuickFix in colQuickFixes objRecordsetMDB.CursorLocation = adUseClient objRecordsetMDB.Open "SELECT * FROM Patching" , objConnectionMDB ,adOpenStatic, adLockOptimistic if objQuickFix.Description = "" then else objRecordsetMDB.AddNew objRecordsetMDB("computer") = strComputer objRecordsetMDB("description") = objQuickFix.Description objRecordsetMDB("ID") = objQuickFix.HotFixID objRecordsetMDB("user") = objQuickFix.InstalledBy objRecordsetMDB("date") = date() objRecordsetMDB.Update objRecordsetMDB.Close objRecordsetMDB = Nothing End if Next end sub

Firewall Information

sub fire(strComputer) on error resume next Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set objFirewall = CreateObject("HNetCfg.FwMgr") Set objPolicy = objFirewall.LocalPolicy.CurrentProfile objRecordsetMDB.CursorLocation = adUseClient objRecordsetMDB.Open "SELECT * FROM Firewall" , objConnectionMDB ,adOpenStatic, adLockOptimistic if objPolicy.FirewallEnabled = "" then else objRecordsetMDB.AddNew objRecordsetMDB("computer") = strComputer objRecordsetMDB("activated") = objPolicy.FirewallEnabled objRecordsetMDB("date") = date() objRecordsetMDB.Update objRecordsetMDB.Close objRecordsetMDB = Nothing end if end sub

Disk Information

sub disks(strComputer) on error resume next Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set objFSO = CreateObject("Scripting.FileSystemObject") Set colDrives = objFSO.Drives For Each objDrive in colDrives objRecordsetMDB.CursorLocation = adUseClient objRecordsetMDB.Open "SELECT * FROM Disks" , objConnectionMDB ,adOpenStatic, adLockOptimistic on error resume next if objDrive.DriveLetter = "" or objDrive.DriveLetter = "m" or objDrive.DriveLetter = "w" then ' nada else objRecordsetMDB.AddNew objRecordsetMDB("computer") = strComputer objRecordsetMDB("gateway") = objDrive.DriveLetter objRecordsetMDB("spaceDisk") = objDrive.FreeSpace objRecordsetMDB("date") = date() objRecordsetMDB.Update objRecordsetMDB.Close objRecordsetMDB = Nothing end if Next end sub

Shares Information

sub shares(strComputer) on error resume next Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colShares = objWMIService.ExecQuery("Select * from Win32_Share") For each objShare in colShares on error resume next objRecordsetMDB.CursorLocation = adUseClient objRecordsetMDB.Open "SELECT * FROM shares" , objConnectionMDB ,adOpenStatic, adLockOptimistic objRecordsetMDB.AddNew objRecordsetMDB("computer") = strComputer objRecordsetMDB("share") = objShare.Name objRecordsetMDB("path") = objShare.Path objRecordsetMDB("date") = date() objRecordsetMDB.Update objRecordsetMDB.Close objRecordsetMDB = Nothing Next end sub

Printers Information

sub printers(strComputer) on error resume next Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colInstalledPrinters =  objWMIService.ExecQuery ("Select * from Win32_Printer") For Each objPrinter in colInstalledPrinters on error resume next objRecordsetMDB.CursorLocation = adUseClient objRecordsetMDB.Open "SELECT * FROM printers" , objConnectionMDB ,adOpenStatic, adLockOptimistic objRecordsetMDB.AddNew objRecordsetMDB("computer") = strComputer 'objRecordsetMDB("username") = fUser objRecordsetMDB("printername") = objPrinter.Name objRecordsetMDB("local") = objPrinter.Location objRecordsetMDB("date") = date() objRecordsetMDB.Update objRecordsetMDB.Close objRecordsetMDB = Nothing Next end sub

Users Information

sub users(strComputer) on error resume next Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colSubfolders = objWMIService.ExecQuery ("Associators of {Win32_Directory.Name='c:\Documents and Settings'} " & "Where AssocClass = Win32_Subdirectory " & "ResultRole = PartComponent") For Each objFolder in colSubfolders on error resume next objRecordsetMDB.CursorLocation = adUseClient objRecordsetMDB.Open "SELECT * FROM Users" , objConnectionMDB ,adOpenStatic, adLockOptimistic objRecordsetMDB.AddNew objRecordsetMDB("computer") = strComputer objRecordsetMDB("name") = objFolder.Name objRecordsetMDB.Update objRecordsetMDB.Close objRecordsetMDB = Nothing Next end sub

Event Viewer Information

sub eventviewer(strComputer) on error resume next Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colLoggedEvents = objWMIService.ExecQuery ("Select * from Win32_NTLogEvent Where Logfile = 'system'") For Each objEvent in colLoggedEvents objRecordsetMDB.CursorLocation = adUseClient objRecordsetMDB.Open "SELECT * FROM EventTable" , objConnectionMDB ,adOpenStatic, adLockOptimistic objRecordsetMDB.AddNew objRecordsetMDB("computer") = strComputer objRecordsetMDB("Category") = objEvent.Category objRecordsetMDB("evttype") = "Application" objRecordsetMDB("EventCode") = objEvent.EventCode objRecordsetMDB("Message") = objEvent.Message objRecordsetMDB("RecordNumber") = objEvent.RecordNumber objRecordsetMDB("SourceName") = objEvent.SourceName objRecordsetMDB("TimeWritten") = objEvent.TimeWritten objRecordsetMDB("Type") = objEvent.Type objRecordsetMDB("User") = objEvent.User objRecordsetMDB("day") = date() objRecordsetMDB.Update objRecordsetMDB.Close objRecordsetMDB = Nothing Next ' limpeza event viewer Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colLoggedEvents = objWMIService.ExecQuery ("Select * from Win32_NTLogEvent Where Logfile = 'security'") For Each objEvent in colLoggedEvents objRecordsetMDB.CursorLocation = adUseClient objRecordsetMDB.Open "SELECT * FROM EventTable" , objConnectionMDB ,adOpenStatic, adLockOptimistic objRecordsetMDB.AddNew objRecordsetMDB("computer") = strComputer objRecordsetMDB("Category") = objEvent.Category objRecordsetMDB("evttype") = "Application" objRecordsetMDB("EventCode") = objEvent.EventCode objRecordsetMDB("Message") = objEvent.Message objRecordsetMDB("RecordNumber") = objEvent.RecordNumber objRecordsetMDB("SourceName") = objEvent.SourceName objRecordsetMDB("TimeWritten") = objEvent.TimeWritten objRecordsetMDB("Type") = objEvent.Type objRecordsetMDB("User") = objEvent.User objRecordsetMDB("day") = date() objRecordsetMDB.Update objRecordsetMDB.Close objRecordsetMDB = Nothing Next Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colLoggedEvents = objWMIService.ExecQuery ("Select * from Win32_NTLogEvent Where Logfile = 'Application'") For Each objEvent in colLoggedEvents objRecordsetMDB.CursorLocation = adUseClient objRecordsetMDB.Open "SELECT * FROM EventTable" , objConnectionMDB ,adOpenStatic, adLockOptimistic objRecordsetMDB.AddNew objRecordsetMDB("compnome") = strComputer objRecordsetMDB("Category") = objEvent.Category objRecordsetMDB("evttype") = "Application" objRecordsetMDB("EventCode") = objEvent.EventCode objRecordsetMDB("Message") = objEvent.Message objRecordsetMDB("RecordNumber") = objEvent.RecordNumber objRecordsetMDB("SourceName") = objEvent.SourceName objRecordsetMDB("TimeWritten") = objEvent.TimeWritten objRecordsetMDB("Type") = objEvent.Type objRecordsetMDB("User") = objEvent.User objRecordsetMDB("day") = date() objRecordsetMDB.Update objRecordsetMDB.Close objRecordsetMDB = Nothing Next
' limpa os ficheiros de log dos pc's Set colLogFiles = objWMIService.ExecQuery ("Select * from Win32_NTEventLogFile where LogFileName='Application'") For Each objLogfile in colLogFiles objLogFile.ClearEventLog() Next
Set colLogFiles = objWMIService.ExecQuery ("Select * from Win32_NTEventLogFile where LogFileName='system'") For Each objLogfile in colLogFiles objLogFile.ClearEventLog() Next
Set colLogFiles = objWMIService.ExecQuery ("Select * from Win32_NTEventLogFile where LogFileName='security'") For Each objLogfile in colLogFiles objLogFile.ClearEventLog() Next end sub

The execution of the script starts here.

‘ conects to active directory of domain “yourdomain,com”

 Set objCOmmand.ActiveConnection = objConnectionAD objCommand.CommandText = "Select Name, Location from 'LDAP://dc=yourdomain,dc=com' " & "Where objectClass='computer'" objCommand.Properties("Page Size") = 1000 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE Set objRecordSetAD = objCommand.Execute objRecordSetAD.MoveFirst

‘ Search for all computers in domain and run for each one all the procedures defined before

Do Until objRecordSetAD.EOF strComputer = objRecordSetAD.Fields("Name").Value ' Now you can start gather the information about all computers in certain domain.  computer(strComputer) software(strComputer) printers(strComputer) network(strComputer) os(strComputer) patches(strComputer) fire(strComputer) disks(strComputer) shares(strComputer) users(strComputer) eventviewer(strComputer) objRecordSetAD.MoveNext Loop

‘Close database conection

objConnectionMDB.Close objConnectionAD.Close

Now you can choose what information you want and also if want more just add more subs.

Hope that this script can be useful to your work, please rate and comment.

Advertisements

About rodvars
Been working in IT Services/Consulting for the past 15 years. My main areas of work are planning, development, managing and administration System infrastructures focusing on optimizing user processes, enforcing business security, performance enhancements, high availabilty and infrastucture scalability.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: