Wednesday, November 26, 2008

One More for the Road. Inventory Script I Wrote

Here's a snippet of an inventory script I wrote a while back to help illustrate what I was talking (or blabbering) about in this previous blog post.  Not as diverse as a Don Hite script, but it does help to show how easy it is to build your own inventory report generator.  Oh, and Sam Adams' Cream Stout is mighty tasty! - Enjoy!

Option Explicit
'*****************************************************************
' filename...: inventory.vbs
' author.....: David M. Stein
' date.......: 11/26/2007
' purpose....: query computer to generate inventory log file
' sql........: no thank you. maybe later
'*****************************************************************
Const strComputer  = "."
Const logLocalPath = "c:\temp"
Const logLocalFile = "inventory_%COMPUTERNAME%.xml"
Const logUploadPath  = "\\server\sharename\logs"
Const LogFileEnabled = True
Const LogUploading   = False
Const debugPrinting  = True
Const ForReading = 1
Const ForWriting = 2

Dim objWMIService, label, colItems, objItem, logFilePath
Dim objFSO, objFile, t1, t2

'*****************************************************************
DebugPrint "connecting to local WMI repository"
t1 = Now
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 

If LogFileEnabled = True Then
logFilePath = logLocalPath & "\" & Replace(logLocalFile, "%COMPUTERNAME%", ComputerName())
DebugPrint "opening local log file for output"
DebugPrint "filename is " & LogFilePath
Set objFSO  = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(logFilePath, ForWriting, True)
objFile.WriteLine ""
objFile.WriteLine ""
Else
wscript.echo "(logging is disabled)" & vbCRLF
wscript.echo ""
wscript.echo ""
End If
'*****************************************************************
Audit_ComputerSystem()
Audit_BIOS()
Audit_OS()
Audit_Memory()
Audit_Hotfixes()
'Audit_LogicalDisks()
'Audit_Drivers()
'Audit_LocalUsers()
'Audit_LocalGroups()
'Audit_InstalledSoftware()
'Audit_Services()
'Audit_NetworkAdapters()
'Audit_VideoConfigs()
'*****************************************************************
If LogFileEnabled = True Then
objFile.WriteLine ""
objFile.Close
Set objFile = Nothing
If LogUploading = True Then
TargetPath = logUploadPath & "\" & logLocalFile
objFSO.CopyFile LogFilePath, TargetPath, True
End If
Set objFSO = Nothing
t2 = Now
DebugPrint "inventory scan completed"
DebugPrint "processing time was " & DateDiff("s", t1, t2) & " seconds"
Else
wscript.echo ""
wscript.echo "processing time was " & DateDiff("s", t1, t2) & " seconds"
End If
'*****************************************************************
Sub Audit_ComputerSystem()
DebugPrint "querying computer system"
label = "computersystem"
OpenGroup label
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem in colItems
OpenInstance label
   PrintElement "Name", objItem.Name
   PrintElement "AdminPasswordStatus", objItem.AdminPasswordStatus
   PrintElement "AutomaticManagedPagefile", objItem.AutomaticManagedPagefile
   PrintElement "AutomaticResetBootOption", objItem.AutomaticResetBootOption
   PrintElement "AutomaticResetCapability", objItem.AutomaticResetCapability
   PrintElement "BootOptionOnLimit", objItem.BootOptionOnLimit
   PrintElement "BootOptionOnWatchDog", objItem.BootOptionOnWatchDog
   PrintElement "BootROMSupported", objItem.BootROMSupported
   PrintElement "BootupState", objItem.BootupState
   PrintElement "Caption", objItem.Caption
   PrintElement "ChassisBootupState", objItem.ChassisBootupState
   PrintElement "CreationClassName", objItem.CreationClassName
   PrintElement "CurrentTimeZone", objItem.CurrentTimeZone
   PrintElement "DaylightInEffect", objItem.DaylightInEffect
   PrintElement "Description", objItem.Description
   PrintElement "DNSHostName", objItem.DNSHostName
   PrintElement "Domain", objItem.Domain
   PrintElement "DomainRole", objItem.DomainRole
   PrintElement "EnableDaylightSavingsTime", objItem.EnableDaylightSavingsTime
   PrintElement "FrontPanelResetStatus", objItem.FrontPanelResetStatus
   PrintElement "InfraredSupported", objItem.InfraredSupported
   PrintElement "InstallDate", objItem.InstallDate
   PrintElement "KeyboardPasswordStatus", objItem.KeyboardPasswordStatus
   PrintElement "LastLoadInfo", objItem.LastLoadInfo
   PrintElement "Manufacturer", objItem.Manufacturer
   PrintElement "Model", objItem.Model
   PrintElement "NameFormat", objItem.NameFormat
   PrintElement "NetworkServerModeEnabled", objItem.NetworkServerModeEnabled
   PrintElement "NumberOfLogicalProcessors", objItem.NumberOfLogicalProcessors
   PrintElement "NumberOfProcessors", objItem.NumberOfProcessors
   PrintElement "PartOfDomain", objItem.PartOfDomain
   PrintElement "PauseAfterReset", objItem.PauseAfterReset
   PrintElement "PCSystemType", objItem.PCSystemType
   PrintElement "PowerManagementSupported", objItem.PowerManagementSupported
   PrintElement "PowerOnPasswordStatus", objItem.PowerOnPasswordStatus
   PrintElement "PowerState", objItem.PowerState
   PrintElement "PowerSupplyState", objItem.PowerSupplyState
   PrintElement "PrimaryOwnerContact", objItem.PrimaryOwnerContact
   PrintElement "PrimaryOwnerName", objItem.PrimaryOwnerName
   PrintElement "ResetCapability", objItem.ResetCapability
   PrintElement "ResetCount", objItem.ResetCount
   PrintElement "ResetLimit", objItem.ResetLimit
   PrintElement "Status", objItem.Status
   PrintElement "SystemStartupDelay", objItem.SystemStartupDelay
   PrintElement "SystemStartupSetting", objItem.SystemStartupSetting
   PrintElement "SystemType", objItem.SystemType
   PrintElement "ThermalState", objItem.ThermalState
   PrintElement "TotalPhysicalMemory", objItem.TotalPhysicalMemory
   PrintElement "UserName", objItem.UserName
   PrintElement "WakeUpType", objItem.WakeUpType
   PrintElement "Workgroup", objItem.Workgroup
   CloseInstance label
Next
CloseGroup label
End Sub

Sub Audit_BIOS()
DebugPrint "querying bios"
label = "bios_setting"
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_BIOS",,48) 
OpenGroup label
For Each objItem in colItems
OpenInstance label
   PrintElement "Name", objItem.Name
   PrintElement "BuildNumber", objItem.BuildNumber
'    PrintElement "Caption", objItem.Caption
   PrintElement "CodeSet", objItem.CodeSet
   PrintElement "CurrentLanguage", objItem.CurrentLanguage
'    PrintElement "Description", objItem.Description
   PrintElement "IdentificationCode", objItem.IdentificationCode
   PrintElement "InstallableLanguages", objItem.InstallableLanguages
   PrintElement "InstallDate", objItem.InstallDate
   PrintElement "LanguageEdition", objItem.LanguageEdition
   PrintElement "Manufacturer", objItem.Manufacturer
   PrintElement "OtherTargetOS", objItem.OtherTargetOS
   PrintElement "PrimaryBIOS", objItem.PrimaryBIOS
   PrintElement "ReleaseDate", objItem.ReleaseDate
   PrintElement "SerialNumber", objItem.SerialNumber
   PrintElement "SMBIOSBIOSVersion", objItem.SMBIOSBIOSVersion
   PrintElement "SMBIOSMajorVersion", objItem.SMBIOSMajorVersion
   PrintElement "SMBIOSMinorVersion", objItem.SMBIOSMinorVersion
   PrintElement "SMBIOSPresent", objItem.SMBIOSPresent
   PrintElement "SoftwareElementID", objItem.SoftwareElementID
   PrintElement "SoftwareElementState", objItem.SoftwareElementState
   PrintElement "Status", objItem.Status
   PrintElement "TargetOperatingSystem", objItem.TargetOperatingSystem
   PrintElement "Version", objItem.Version
Next
CloseGroup label
End Sub

Sub Audit_OS()
DebugPrint "querying operating system"
label = "operating_system"
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem",,48) 
OpenGroup label
For Each objItem in colItems 
OpenInstance label
   PrintElement "Name", objItem.Name
   PrintElement "BootDevice", objItem.BootDevice
   PrintElement "BuildNumber", objItem.BuildNumber
   PrintElement "BuildType", objItem.BuildType
   PrintElement "Caption", objItem.Caption
   PrintElement "CodeSet", objItem.CodeSet
   PrintElement "CountryCode", objItem.CountryCode
   PrintElement "CreationClassName", objItem.CreationClassName
   PrintElement "CSCreationClassName", objItem.CSCreationClassName
   PrintElement "CSDVersion", objItem.CSDVersion
   PrintElement "CSName", objItem.CSName
   PrintElement "CurrentTimeZone", objItem.CurrentTimeZone
   PrintElement "DataExecutionPrevention_32BitApplications", objItem.DataExecutionPrevention_32BitApplications
   PrintElement "DataExecutionPrevention_Available", objItem.DataExecutionPrevention_Available
   PrintElement "DataExecutionPrevention_Drivers", objItem.DataExecutionPrevention_Drivers
   PrintElement "DataExecutionPrevention_SupportPolicy", objItem.DataExecutionPrevention_SupportPolicy
   PrintElement "Debug", objItem.Debug
   PrintElement "Description", objItem.Description
   PrintElement "Distributed", objItem.Distributed
   PrintElement "EncryptionLevel", objItem.EncryptionLevel
   PrintElement "ForegroundApplicationBoost", objItem.ForegroundApplicationBoost
   PrintElement "FreePhysicalMemory", objItem.FreePhysicalMemory
   PrintElement "FreeSpaceInPagingFiles", objItem.FreeSpaceInPagingFiles
   PrintElement "FreeVirtualMemory", objItem.FreeVirtualMemory
   PrintElement "InstallDate", objItem.InstallDate
   PrintElement "LargeSystemCache", objItem.LargeSystemCache
   PrintElement "LastBootUpTime", objItem.LastBootUpTime
   PrintElement "LocalDateTime", objItem.LocalDateTime
   PrintElement "Locale", objItem.Locale
   PrintElement "Manufacturer", objItem.Manufacturer
   PrintElement "MaxNumberOfProcesses", objItem.MaxNumberOfProcesses
   PrintElement "MaxProcessMemorySize", objItem.MaxProcessMemorySize
   PrintElement "NumberOfLicensedUsers", objItem.NumberOfLicensedUsers
   PrintElement "NumberOfProcesses", objItem.NumberOfProcesses
   PrintElement "NumberOfUsers", objItem.NumberOfUsers
   PrintElement "OperatingSystemSKU", objItem.OperatingSystemSKU
   PrintElement "Organization", objItem.Organization
   PrintElement "OSArchitecture", objItem.OSArchitecture
   PrintElement "OSLanguage", objItem.OSLanguage
   PrintElement "OSProductSuite", objItem.OSProductSuite
   PrintElement "OSType", objItem.OSType
   PrintElement "OtherTypeDescription", objItem.OtherTypeDescription
   PrintElement "PAEEnabled", objItem.PAEEnabled
   PrintElement "PlusProductID", objItem.PlusProductID
   PrintElement "PlusVersionNumber", objItem.PlusVersionNumber
   PrintElement "Primary", objItem.Primary
   PrintElement "ProductType", objItem.ProductType
   PrintElement "QuantumLength", objItem.QuantumLength
   PrintElement "QuantumType", objItem.QuantumType
   PrintElement "RegisteredUser", objItem.RegisteredUser
   PrintElement "SerialNumber", objItem.SerialNumber
   PrintElement "ServicePackMajorVersion", objItem.ServicePackMajorVersion
   PrintElement "ServicePackMinorVersion", objItem.ServicePackMinorVersion
   PrintElement "SizeStoredInPagingFiles", objItem.SizeStoredInPagingFiles
   PrintElement "Status", objItem.Status
   PrintElement "SuiteMask", objItem.SuiteMask
   PrintElement "SystemDevice", objItem.SystemDevice
   PrintElement "SystemDirectory", objItem.SystemDirectory
   PrintElement "SystemDrive", objItem.SystemDrive
   PrintElement "TotalSwapSpaceSize", objItem.TotalSwapSpaceSize
   PrintElement "TotalVirtualMemorySize", objItem.TotalVirtualMemorySize
   PrintElement "TotalVisibleMemorySize", objItem.TotalVisibleMemorySize
   PrintElement "Version", objItem.Version
   PrintElement "WindowsDirectory", objItem.WindowsDirectory
   CloseInstance label
Next
CloseGroup label
End Sub

Sub Audit_Memory()
DebugPrint "querying memory"
label = "memory_device"
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PhysicalMemory",,48) 
OpenGroup label
For Each objItem in colItems 
OpenInstance label
   PrintElement "BankLabel", objItem.BankLabel
   PrintElement "Capacity", objItem.Capacity
   PrintElement "Caption", objItem.Caption
   PrintElement "CreationClassName", objItem.CreationClassName
   PrintElement "DataWidth", objItem.DataWidth
   PrintElement "Description", objItem.Description
   PrintElement "DeviceLocator", objItem.DeviceLocator
   PrintElement "FormFactor", objItem.FormFactor
   PrintElement "HotSwappable", objItem.HotSwappable
   PrintElement "InstallDate", objItem.InstallDate
   PrintElement "InterleaveDataDepth", objItem.InterleaveDataDepth
   PrintElement "InterleavePosition", objItem.InterleavePosition
   PrintElement "Manufacturer", objItem.Manufacturer
   PrintElement "MemoryType", objItem.MemoryType
   PrintElement "Model", objItem.Model
   PrintElement "Name", objItem.Name
   PrintElement "OtherIdentifyingInfo", objItem.OtherIdentifyingInfo
   PrintElement "PartNumber", objItem.PartNumber
   PrintElement "PositionInRow", objItem.PositionInRow
   PrintElement "PoweredOn", objItem.PoweredOn
   PrintElement "Removable", objItem.Removable
   PrintElement "Replaceable", objItem.Replaceable
   PrintElement "SerialNumber", objItem.SerialNumber
   PrintElement "SKU", objItem.SKU
   PrintElement "Speed", objItem.Speed
   PrintElement "Status", objItem.Status
   PrintElement "Tag", objItem.Tag
   PrintElement "TotalWidth", objItem.TotalWidth
   PrintElement "TypeDetail", objItem.TypeDetail
   PrintElement "Version", objItem.Version
   CloseInstance label
Next
CloseGroup label
End Sub

Sub Audit_Hotfixes()
DebugPrint "querying hotfixes"
label = "hotfix"
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_QuickFixEngineering",,48) 
OpenGroup label
For Each objItem in colItems
OpenInstance label
   PrintElement "HotFixID", objItem.HotFixID
   PrintElement "Caption", objItem.Caption
   PrintElement "CSName", objItem.CSName
   PrintElement "Description", objItem.Description
   PrintElement "FixComments", objItem.FixComments
   PrintElement "InstallDate", objItem.InstallDate
   PrintElement "InstalledBy", objItem.InstalledBy
   PrintElement "InstalledOn", objItem.InstalledOn
   PrintElement "Name", objItem.Name
   PrintElement "ServicePackInEffect", objItem.ServicePackInEffect
   PrintElement "Status", objItem.Status
   CloseInstance label
Next
CloseGroup label
End Sub
'*****************************************************************
Function ComputerName()
Dim wshNetwork, retval
Set wshNetwork = CreateObject("Wscript.Network")
retval = wshNetwork.ComputerName
Set wshNetwork = Nothing
ComputerName = retval
End Function
'*****************************************************************
Sub OpenGroup(v)
If LogFileEnabled = True Then
objFile.WriteLine "<" & v & "s>"
Else
wscript.echo "<" & v & "s>"
End If
End Sub
'*****************************************************************
Sub CloseGroup(v)
If LogFileEnabled = True Then
objFile.WriteLine ""
Else
wscript.echo ""
End If
End Sub
'*****************************************************************
Sub OpenInstance(v)
If LogFileEnabled = True Then
objFile.WriteLine vbTab & "<" & v & ">"
Else
wscript.echo vbTab & "<" & v & ">"
End If
End Sub
'*****************************************************************
Sub CloseInstance(v)
If LogFileEnabled = True Then
objFile.WriteLine vbTab & ""
Else
wscript.echo vbTab & ""
End If
End Sub
'*****************************************************************
Sub PrintElement(strTag, strValue)
If LogFileEnabled = True Then
objFile.WriteLine vbTab & vbTab & ""
Else
wscript.echo vbTab & vbTab & ""
End If
End Sub
'*****************************************************************
Sub DebugPrint(s)
If DebugPrinting = True Then
wscript.echo Now & vbTab & s
End If
End Sub

Post a Comment