Monday, December 04, 2006

WMI: AD-COIT v2.3 (Computer Object Intentory Tool)

Updated: 1/15/07, For the latest release information go here.

Updated: 1/9/07, Instead of struggling with the extra characters and formatting issues associated with posting long scripts to blogger (see the comments below), I created a project for AD-COIT over on Sourceforge. From there, you can download the entire working script. While I plan to post information and updates to the blog, you’ll be able to find the latest release there. I'll leave the code posted below - so that you can review the logic and skim though it easily. However, if you want a copy of it...

Download the latest version of AD-COIT.


12/4/06, It's been a while since I've posted an update to my AD Workstation Inventory script, so the other day when I was using it to troubleshoot antivirus-definition updates on a few clients, I figured now would be as good of time as any to post my changes (and to do a name-change to "AD-COIT" for "Active Directory Computer Object Inventory Tool").

In any case, refer here for basic usage and instructions - as they've not really changed since my last update. You should be able to just copy and paste the below code snippet into notepad and save it with a .VBS extension. Let me know if you have any trouble doing so.

Otherwise, if you're interested in the logic or how I came up with the functions, you can check out my WMI category. If you're interested in the output, I'll post that in a follow-up.


' AD Computer Object Inventory Tool v2.3, by wmi.wmi@gmail.com
'------------------------------
' Usage: "cscript scriptname.vbs >inventoryreport.txt"

'=== Begin Script Configuration ===
strLdapPath = "'LDAP://DC=domain,DC=local'" 'Set to relflect your LDAP path
'=== End Script Configuration ===

Dim rootDSE
Dim DistinguishedName
Dim objShell,objDef,objDate,objVer,objRev,objOutFile,objFSO,objNDate,objToday, strRole
Const MegabyteConversion = 1048576
Const Warning_Threshold = 1000
Set objShell = CreateObject("WScript.Shell")


wscript.echo strLdapPath
dsqueryFSMOroles

'Set rootDSE = GetObject("LDAP://rootDSE")
'strLdapPath = "'LDAP://" & rootDSE.Get("defaultNamingContext")'"
'wscript.echo strLdapPath

On error resume next
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 " & strLdapPath & " Where objectCategory='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strComputer = objRecordSet.Fields("Name").Value
'Set objShell = CreateObject("WScript.Shell")
strCommand = "%comspec% /c ping -n 3 -w 1000 " & strComputer & ""
Set objExecObject = objShell.Exec(strCommand)
strText=""
Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadAll()
If Instr(strText, "Reply") > 0 Then
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
If Err.Number > 0 then
strErrorSystems = strComputer & ", " & strErrorSystems
else
BuildHeader
OperatingSystem
BiosQuery
ProcQuery(strComputer)
DayLightSavingsEffect(strComputer)
SavDate(strComputer)
Diskspace
ListInstalledApplications
BuildFooter
end if
Err.Clear 'flushes error code from previous loop
Else
UnavailableSystems = strComputer & ", " & UnavailableSystems

End If
Loop
objRecordSet.MoveNext
Loop
Wscript.Echo "The following systems were unavailable: " & UnavailableSystems
Wscript.Echo " "
Wscript.Echo "The following systems were on, but returned an error: " & strErrorSystems


Function ListInstalledApplications
Set colItemsProduct = objWMIService.ExecQuery("Select * from Win32_Product")
Wscript.echo "Workstation Application List:"
Wscript.Echo "-----------------------------"
For Each objItemProduct in colItemsProduct
Wscript.echo objItemProduct.Description & " (v." & objItemProduct.Version & ")"
'Wscript.echo "Location: " & objItem.InstallLocation & ", "
Next

End Function

Function dsqueryFSMOroles
Dim objShell, ArrFSMOCmds(4)
Set objShell = CreateObject("WScript.Shell")

Cmd1 = "cmd /c"
Cmd2 = "dsquery server -hasfsmo "
ArrFSMOCmds(0) = "schema"
ArrFSMOCmds(1) = "rid"
ArrFSMOCmds(2) = "name"
ArrFSMOCmds(3) = "infr"
ArrFSMOCmds(4) = "PDC"

For Each ArrFSMOCmds in ArrFSMOCmds
Set objExec = objShell.Exec(Cmd1 & Cmd2 & ArrFSMOCmds)
strExecResults = LCase(objExec.StdOut.ReadAll)
Wscript.Echo ArrFSMOCmds & ": " & strExecResults
Next

End Function


Function BuildHeader
Wscript.Echo "-------------------------------------------------------------"
Wscript.Echo "Computer Name: " & strComputer
Wscript.Echo "-------------------------------------------------------------"
End Function
Function OperatingSystem
Set colItems = objWMIService.ExecQuery ("Select * From Win32_OperatingSystem")
For Each objItem in ColItems
strOs = objItem.Caption
strServPack = objItem.CSDVersion
strInstallDate = objItem.InstallDate
Next
Wscript.Echo "Operating System: " & strOs
Wscript.Echo "Service Pack: " & strServPack
Wscript.Echo "Install Date: " & WMIDateStringToDate(strInstallDate)
End Function
Function BiosQuery
Set colItems2 = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
For Each objItem in colItems2
strDellTag = objItem.SerialNumber
strManu = objItem.Manufacturer
strBiosName = objitem.Name
Next
WScript.Echo "Manufacturer: " & strManu
Wscript.Echo "BIOS Name: " & strBiosName
Wscript.Echo "Service Tag: " & strDellTag
End Function
Function WMIDateStringToDate(dtmDate)
WScript.Echo dtm:
WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
& " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))
End Function
Function ProcQuery(shost)
Set colItems4 = objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem in colitems4
strProcessor = objItem.Name
Next
Wscript.Echo "Processor type: " & strProcessor
End Function
Function DayLightSavingsEffect(shost)
Set colItems3 = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)

For Each objItem in colitems3
strDomainRole = objItem.DomainRole
strUserName = objItem.Username
strModel = objItem.Model
strRAM = objItem.TotalPhysicalMemory
strTimeZone = (objItem.CurrentTimeZone / 60)
strDayLightSavings = objItem.DaylightInEffect
Next

Select Case strDomainRole
Case 0
strRole = "Standalone workstation"
Case 1
strRole = "Member workstation"
Case 2
strRole = "Standalone server"
Case 3
strRole = "Member server"
Case 4
strRole = "Backup domain controller"
Case 5
strRole = "PDC Emulator"
End Select
Wscript.Echo "Domain Role: " & strRole
Wscript.Echo "Current User: " & strUsername
Wscript.Echo "Model: " & strModel
Wscript.Echo "RAM: " & strRAM
Wscript.Echo "Daylight Savings in effect: " & strDayLIghtSavings
Wscript.Echo "Time Zone: " & strTimeZone
strUsername = ""
End Function
Function SavDate(shost)
strCmdRun = "cmd /c"
strRegQ = "reg query "
strRegKey = "\HKLM\SOFTWARE\Symantec\SharedDefs\"
strCmdSw = " /v "
strRegKey2 = "DEFWATCH_10"
Set objExec = objShell.Exec(strCmdRun & strRegQ & "\\" & shost & strRegKey & strCmdSw & strRegKey2)
strExecResults = LCase(objExec.StdOut.ReadAll)
objVer = Right(strExecResults,16)
objRev = Right(objVer,7)
objDate = Left(objVer,8)
objYear = Left(objDate,4)
objMonth = Mid(objDate,5,2)
objDay = Right(objDate,2)
objNDate = CDATE(objMonth &"/"& objDay &"/"& objYear)
Wscript.Echo "Symantec Antivirus Definition Date: " & objNDate &" Rev. "& objRev & " "
End Function
Function DiskSpace
Set colLogicalDisk = objWMIService.InstancesOf("Win32_LogicalDisk")
If colLogicalDisk.Count = 0 Then
Wscript.Echo "No drives exist on this computer."
Else
For Each objLogicalDisk in colLogicalDisk
FreeMegaBytes = objLogicalDisk.Freespace / MegabyteConversion

'If objLogicalDisk.DriveType = 3 Then
If FreeMegaBytes <> 0 Then
FreeMegaBytes_String = Int(FreeMegaBytes) & " MB Free"
Wscript.Echo objLogicalDisk.DeviceID & ", " & FreeMegaBytes_String &amp;amp;amp; ", " & objLogicalDisk.Description
End if
Next
End if
End Function
Function BuildFooter
Wscript.Echo "-------------------------------------------------------------"
End Function


4 comments:

Doug Steinschneider said...

I'm just familiar enough with VBS to know those &'s look like something blogger added but not enough to know how to fix it.
---------------------------
Windows Script Host
---------------------------
Script: L:\_IT Admin\ADCOIT.VBS
Line: 134
Char: 52
Error: Expected ')'
Code: 800A03EE
Source: Microsoft VBScript compilation error

---------------------------
OK
---------------------------

Anonymous said...

now I see that blogger changed my post - it needs a code entry mode :)

Nick said...

Thanks for the heads-up! It looks like I'm etting some garbage on a few lines (194, 195, 208, 209) when I copy it into blogger... I'll look into this further... in the mean time, if anyone needs a copy of the script, email me and I'll send it over(wmi.wmi@gmail.com).

Nick said...

So I've been playing with this trying to get it to look right on blogger – but to no avail. So… I created a project on Source Forge, and as soon as the registration is approved (2-3 days), I'll post it there for download.

In the mean time, just email me if you want the script.