Introduction
This script will remotely query and gather information from PCs in a subnet using IP & WMI. Additionally, it will check for devices and use their Embedded Web Server to attempt to identify what they are. The results will be saved into an Excel spreadsheet.
Background
Based on the script AssetScan.vbs submitted by Sean Kelly - skelly@engineer.com. rev 12 April 2005. HEAVILY modified.
Using the code
Just copy to code below, or download the source file and run. Use the parameters to change the outputed fields and / or place the script into interactive mode.
Option Explicit
If (InStr(LCase(WScript.FullName),"wscript") = 0) Then Call subCheckScriptHost()
Const DEFAULT_PATH = "P:\Asset Scans\"
Const REPORT_TITLE = "Subnet Scan"
Const LINE_HEADER = "<br />---- "
Dim GintRow
Const FLAG_RETURN_IMMEDIATELY = &h10 : Const FLAG_FORWARD_ONLY = &h20
Dim GlAskForInput, GlShortFormat
Dim GaryIPRange(2)
Dim GobjExcel : Set GobjExcel = Nothing
Dim GobjIE : Set GobjIE = Nothing
On Error Resume Next
Err.Clear
Set GobjExcel = WScript.CreateObject("Excel.Application")
Set GobjIE = WScript.CreateObject("InternetExplorer.Application")
On Error Goto 0
If Err.Number <> 0 Then
Call subCloseApp("Run time Error.", Err.Number, Err.Description, Err.Source)
ElseIf (GobjExcel Is Nothing) Then
Call subCloseApp("Fatal Error creating Excel object", Err.Number, Err.Description, Err.Source)
ElseIf (GobjIE Is Nothing) Then
Call subCloseApp("Fatal Error creating IE object", Err.Number, Err.Description, Err.Source)
End If
Dim strArgument
Dim objArguments : Set objArguments = Nothing
On Error Resume Next
Err.Clear
Set objArguments = WScript.Arguments
On Error Goto 0
If Err.Number <> 0 Then
Call subCloseApp("Run time Error.", Err.Number, Err.Description, Err.Source)
ElseIf (GobjExcel Is Nothing) Then
Call subCloseApp("Fatal Error creating Excel object", Err.Number, Err.Description, Err.Source)
ElseIf (GobjIE Is Nothing) Then
Call subCloseApp("Fatal Error creating IE object", Err.Number, Err.Description, Err.Source)
End If
GlAskForInput = False : GlShortFormat = False
For Each strArgument In objArguments
Select Case LCase(strArgument)
Case "-input", "/input", "-i", "/i"
GlAskForInput = True
Case "-short", "/short", "-s", "/s"
GlShortFormat = True
Case Else
MsgBox "Invalid Parameter: " & strArgument & ". Running in default mode.", vbInformation + vbOKOnly, REPORT_TITLE
End Select
Next
Dim GintPC_IP, GintPC_Name, GintPC_Make, GintPC_Model, GintPC_Serial, GintPC_User
If GlShortFormat Then
GintPC_IP = 0 : GintPC_Name = 1 : GintPC_Make = 2 : GintPC_Model = 3 : GintPC_Serial = 4 : GintPC_User = 5
Else
Dim GintPC_Role, GintPC_MAC, GintPC_RAM, GintPC_OS, GintPC_BIOS, GintPC_CPU, GintPC_Speed, GintPC_Date, GintPC_Admins, _
GintPC_SMS, GintPC_C_Size, GintPC_C_Free, GintPC_D_Size, GintPC_D_Free, GintPC_E_Size, GintPC_E_Free, GintPC_NIC_1, _
GintPC_NIC_2, GintPC_NIC_3, GintPC_NIC_4, GintPC_NIC_5
GintPC_IP = 0 : GintPC_Name = 1 : GintPC_Role = 2 : GintPC_Make = 3 : GintPC_Model = 4
GintPC_MAC = 5 : GintPC_Serial = 6 : GintPC_RAM = 7 : GintPC_OS = 8 : GintPC_BIOS = 9
GintPC_CPU = 10 : GintPC_Speed = 11 : GintPC_User = 12 : GintPC_Date = 13 : GintPC_Admins = 14
GintPC_SMS = 15 : GintPC_C_Size = 16 : GintPC_C_Free = 17 : GintPC_D_Size = 18 : GintPC_D_Free = 19
GintPC_E_Size = 20 : GintPC_E_Free = 21 : GintPC_NIC_1 = 22 : GintPC_NIC_2 = 23 : GintPC_NIC_3 = 24
GintPC_NIC_4 = 25 : GintPC_NIC_5 = 26
End If
Dim strDefaultFile, strStart, strIPList
strDefaultFile = funSetThingsUp(strStart)
If GlAskForInput Then If (MsgBox("Run System Auditor?", vbQuestion + vbYesNo, REPORT_TITLE) = vbNo) Then WScript.Quit
Call subShowBar()
If GlAskForInput Then GobjIE.Document.ParentWindow.Document.Script.ListOP("Application Started: " & strStart)
strIPList = funIPCreate()
Call subBuildXLS()
Call subConnect(strIPList)
Call subFooter()
GobjIE.Quit
GobjExcel.Visible = True
If GlAskForInput Then strDefaultFile = strDefaultFile & "i"
If GlShortFormat Then strDefaultFile = strDefaultFile & "s"
If funSaveFiles(strDefaultFile) Then MsgBox "Your inventory run is complete!", vbInformation + vbOKOnly, REPORT_TITLE
Set GobjIE = Nothing : Set GobjExcel = Nothing
WScript.Quit
Sub subCheckScriptHost()
Const WINDOW_HIDE = 0
Dim objShell : Set objShell = Nothing
Dim strExec
On Error Resume Next
Err.Clear
Set objShell = CreateObject("WScript.Shell")
On Error Goto 0
If Err.Number <> 0 Then
Call subCloseApp("Run time Error.", Err.Number, Err.Description, Err.Source)
ElseIf (objShell Is Nothing) Then
Call subCloseApp("Fatal Error creating Shell object", Err.Number, Err.Description, Err.Source)
End If
strExec ="%COMSPEC% /c " & Chr(34) & "wscript.exe //NoLogo " & Chr(34) & WScript.ScriptFullName & Chr(34) & Chr(34)
objShell.Run strExec, WINDOW_HIDE, False
Wscript.Quit
End Sub
Sub subCloseApp(strError, intError, strDescription, strSource)
On Error Resume Next
Dim strMessage
GobjIE.Quit
GobjExcel.Visible = True
Set GobjIE = Nothing : Set GobjExcel = Nothing
strMessage = strError
If strError > 0 Then strMessage = strMessage & vbCrLf & vbCrLf & _
"*** UNRECOVERABLE ERROR: ABORTING ***" & vbCrLf & _
"*************************************" & vbCrLf & _
" Error: " & intError & vbCrLf & _
" Description : " & strDescription & vbCrLf & _
" Source: " & strSource
MsgBox strMessage, vbInformation + vbOKOnly, REPORT_TITLE
WScript.Quit
On Error Goto 0
End Sub
Sub subShowBar()
Dim intWindowWidth, intWindowHeight
On Error Resume Next
Err.Clear
GobjIE.Navigate("about:blank")
On Error Goto 0
If Err.Number <> 0 Then Call subCloseApp("Error navigating to 'about:blank'.", Err.Number, Err.Description, Err.Source)
Do : WScript.Sleep 50 : Loop Until GobjIE.ReadyState = 4
With GobjIE.Document.ParentWindow.Screen
intWindowHeight = .AvailHeight
intWindowWidth = .AvailWidth
End With
With GobjIE
.FullScreen = True
.Toolbar = False
.StatusBar = False
.AddressBar = False
.Resizable = False
.Width = 420
.Left = (intWindowWidth - 420) \ 2
If GlAskForInput Then
.Height = 270
.Top = (intWindowHeight - 270) \ 2
Else
.Height = 100
.Top = (intWindowHeight - 100) \ 2
End If
With .Document
.WriteLN("<!DOCTYPE HTML PUBLIC>")
.WriteLN("<HTML " & _
" Style = ""border-style:outset;" & _
"border-width:4px"" " & _
"OnKeyDown = ""VBScript:SuppressKeys"" " & _
"onHelp = ""VBScript:SuppressIeFns"" " & _
"onContextMenu = ""VBScript:SuppressIeFns"">")
.WriteLN( "<HEAD>")
.WriteLN( "<TITLE>" & _
REPORT_TITLE & _
"</TITLE>")
.WriteLN( "<STYLE " & _
"Type = ""text/css"">")
.WriteLN( "Body {background-color:#ece9d8;" & _
"text-align:center;" & _
"vertical-align:middle}")
.WriteLN( "</STYLE>")
.WriteLN( "<SCRIPT " & _
"Language = ""VBScript"">")
.WriteLN( "Function SuppressKeys()")
.WriteLN( "If NOT CBool(Window.Event.CTRLKey) Then")
.WriteLN( "Exit Function")
.WriteLN( "End If")
.WriteLN( "Window.Event.KeyCode = 0")
.WriteLN( "Window.Event.CancelBubble = True")
.WriteLN( "Window.Event.ReturnValue = False")
.WriteLN( "End Function")
.WriteLN( "Function SuppressIEFns()")
.WriteLN( "Window.Event.CancelBubble = True")
.WriteLN( "Window.Event.ReturnValue = False")
.WriteLN( "End Function")
.WriteLN( "Function BarOP(intPercent)")
.WriteLN( "Window.BarArea.Style.Width = intPercent & ""%""")
.WriteLN( "End Function")
If GlAskForInput Then
.WriteLN( "Function ListOP(strToInsert)")
.WriteLN( "Window.DataArea.InsertAdjacentHtml ""beforeBegin"", strToInsert")
.WriteLN( "Window.DataArea.ScrollIntoView")
.WriteLN( "End Function")
End If
.WriteLN( "</SCRIPT>")
.WriteLN( "</HEAD>")
.WriteLN( "<BODY " & _
"Scroll = ""No"">")
.WriteLN( "<TABLE>")
.WriteLN( "<TR>")
.WriteLN( "<TD Style = ""text-align:center;" & _
"font-family:Arial;font-size:16pt;" & _
"font-weight:bold"">")
.WriteLN( "Premera Blue Cross - Auditor")
.WriteLN( "</TD>")
.WriteLN( "</TR>")
.WriteLN( "<TR>")
.WriteLN( "<TD ID = ""barcell"" " & _
"Style = ""width:400px;" & _
"padding-left:7px;" & _
"padding-right:7px;" & _
"text-align:left;" & _
"border-style:inset;" & _
"border-width:thin;" & _
"background-color:navajowhite"">")
.WriteLN( "<HR ID = ""BarArea"" " & _
"Style = ""width:0%;height:15px;" & _
"color:darkblue"" />")
.WriteLN( "</TD>")
.WriteLN( "</TR>")
If GlAskForInput Then
.WriteLN( "<TR>")
.WriteLN( "<TD STYLE = ""padding-top:15px"">")
.WriteLN( "<DIV ID = ""progresslist"" " & _
"Style = ""height:100px;width:380px;" & _
"max-height:100%;max-width:100%;" & _
"padding-left:10px;text-align:left;" & _
"font-family:Arial;font-size:10pt;" & _
"font-weight:bold;border-style:inset;" & _
"border-width:thin;overflow:scroll"">")
.WriteLN( "<SPAN " & _
"ID = ""DataArea"">" & _
"</SPAN>")
.WriteLN( "</DIV>")
.WriteLN( "</TD>")
.WriteLN( "</TR>")
.WriteLN( "<TR>")
.WriteLN( "<TD STYLE = ""padding-top:20px;" & _
"width:400px;font-family:Arial;" & _
"font-size:10pt;" & _
"font-weight:bold"">")
.WriteLN( "Scanning for systems...")
.WriteLN( "</TD>")
.WriteLN( "</TR>")
End If
.WriteLN( "</TABLE>")
.WriteLN( "</BODY>")
.WriteLN("</HTML>")
End With
.Visible = True
End With
End Sub
Sub subBuildXLS()
Const EXCEL_WHITE = 2 : Const EXCEL_BLUE = 11 : Const EXCEL_SOLID = 1 : Const EXCEL_LEFT = 2 : Const EXCEL_RIGHT = 4
Dim aryPCs()
If GlShortFormat Then
ReDim aryPCs(5)
aryPCs(GintPC_IP) = "IP Address" : aryPCs(GintPC_Name) = "Hostname" : aryPCs(GintPC_Make) = "Make"
aryPCs(GintPC_Model) = "Model" : aryPCs(GintPC_Serial) = "Serial Number" : aryPCs(GintPC_User) = "Logged User"
Else
ReDim aryPCs(26)
aryPCs(GintPC_IP) = "IP Address" : aryPCs(GintPC_Name) = "Hostname"
aryPCs(GintPC_Role) = "Role" : aryPCs(GintPC_Make) = "Make"
aryPCs(GintPC_Model) = "Model" : aryPCs(GintPC_MAC) = "MAC Address"
aryPCs(GintPC_Serial) = "Serial Number" : aryPCs(GintPC_RAM) = "RAM"
aryPCs(GintPC_OS) = "Operation System" : aryPCs(GintPC_BIOS) = "BIOS Revision"
aryPCs(GintPC_CPU) = "CPU Type" : aryPCs(GintPC_Speed) = "CPU Speed"
aryPCs(GintPC_User) = "Logged User" : aryPCs(GintPC_Date) = "Date Installed"
aryPCs(GintPC_Admins) = "Local Admins" : aryPCs(GintPC_SMS) = "SMS Site"
aryPCs(GintPC_C_Size) = "C: Size" : aryPCs(GintPC_C_Free) = "C: Free"
aryPCs(GintPC_D_Size) = "D: Size" : aryPCs(GintPC_D_Free) = "D: Free"
aryPCs(GintPC_E_Size) = "E: Size" : aryPCs(GintPC_E_Free) = "E: Free"
aryPCs(GintPC_NIC_1) = "NIC #1" : aryPCs(GintPC_NIC_2) = "NIC #2"
aryPCs(GintPC_NIC_3) = "NIC #3" : aryPCs(GintPC_NIC_4) = "NIC #4"
aryPCs(GintPC_NIC_5) = "NIC #5"
End If
GintRow = 1
GobjExcel.Visible = False
GobjExcel.WorkBooks.Add
GobjExcel.Sheets("Sheet1").Select()
GobjExcel.Sheets("Sheet1").Name = REPORT_TITLE
GobjExcel.Rows(1).RowHeight = 25
If GlShortFormat Then
GobjExcel.Range("A1:F1").Select
Else
GobjExcel.Range("A1:AA1").Select
End If
GobjExcel.Selection.Font.Size = 8
GobjExcel.Selection.Font.ColorIndex = EXCEL_WHITE
GobjExcel.Selection.Interior.ColorIndex = EXCEL_BLUE
GobjExcel.Selection.Interior.Pattern = EXCEL_SOLID
GobjExcel.Selection.Font.Bold = True
GobjExcel.Selection.WrapText = True
If GlShortFormat Then
GobjExcel.Range("A:F").HorizontalAlignment = EXCEL_LEFT
Else
GobjExcel.Range("A:AA").HorizontalAlignment = EXCEL_LEFT
GobjExcel.Range("H:H" ).HorizontalAlignment = EXCEL_RIGHT
GobjExcel.Range("L:L" ).HorizontalAlignment = EXCEL_RIGHT
GobjExcel.Range("N:N" ).HorizontalAlignment = EXCEL_RIGHT
GobjExcel.Range("Q:V" ).HorizontalAlignment = EXCEL_RIGHT
End If
Call subAddLineXLS(aryPCs)
End Sub
Sub subAddLineXLS(ByRef aryLineDetail)
Dim intCounter, intRows
intRows = UBound(aryLineDetail) + 1
For intCounter = 1 To intRows
GobjExcel.Cells(GintRow, intCounter).Value = Trim(aryLineDetail(intCounter - 1))
Next
GintRow = GintRow + 1
GobjExcel.Cells(1, 1).Select
End Sub
Sub subFooter()
Const EXCEL_BLACK = 1 : Const EXCEL_LEFT = 2
Const IP_SUBNET = 0 : Const IP_START = 1 : Const IP_END = 2
Dim intCounter
Dim strParameters
Dim aryFooters(2)
strParameters = ""
If GlShortFormat Then
strParameters = "Short Format "
GobjExcel.Range("A:F").ColumnWidth() = 40
GobjExcel.Range("A:F").Columns.Autofit
Else
GobjExcel.Range("A:AA").ColumnWidth() = 40
GobjExcel.Range("A:AA").Columns.Autofit
End If
If GlAskForInput Then strParameters = strParameters & "Interactive"
aryFooters(0) = "Premera Blue Cross"
aryFooters(1) = "Inventory AssetScan " & strParameters
aryFooters(2) = "IP Range: " & GaryIPRange(IP_SUBNET) & "." & GaryIPRange(IP_START) & _
" through " & GaryIPRange(IP_SUBNET) & "." & GaryIPRange(IP_END)
GintRow = GintRow + 3
For intCounter = 0 To 2
GintRow = GintRow + 1
GobjExcel.Cells(GintRow, 4).Select
GobjExcel.Selection.Font.ColorIndex = EXCEL_BLACK
GobjExcel.Selection.Font.Size = 8
GobjExcel.Selection.Font.Bold = False
GobjExcel.Selection.HorizontalAlignment = EXCEL_LEFT
GobjExcel.Cells(GintRow, 4).Value = aryFooters(intCounter)
Next
End Sub
Sub subConnect(strAllIPs)
Const ACCESS_DENIED = &H80041003
Const MAX_WAIT = &H80
Const IP_SUBNET = 0 : Const IP_START = 1 : Const IP_END = 2
Dim strTitle, strURL, strMessage, strPage, strTemp
Dim colIPAddresses : Set colIPAddresses = Nothing
Dim colItems : Set colItems = Nothing
Dim objRegularExpression : Set objRegularExpression = New RegExp
Dim objItem : Set objItem = Nothing
Dim objLocator : Set objLocator = Nothing
Dim objSMSClient : Set objSMSClient = Nothing
Dim objMSXML3 : Set objMSXML3 = Nothing
Dim intIPRange, intLoop, intItems
Dim strRunCommand
Dim intPercentage, intOnLine
If GlAskForInput Then Dim strResetLine, strResultLine
Dim lFoundIt
Dim aryPCs()
On Error Resume Next
Err.Clear
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objSMSClient = CreateObject("Microsoft.SMS.Client")
Set objMSXML3 = CreateObject("MSXML2.ServerXMLHTTP")
On Error Goto 0
If Err.Number <> 0 Then
Call subCloseApp("Run time Error.", Err.Number, Err.Description, Err.Source)
ElseIf (objLocator Is Nothing) Then
Call subCloseApp("Fatal Error creating Locator object", Err.Number, Err.Description, Err.Source)
ElseIf (objSMSClient Is Nothing) Then
Call subCloseApp("Fatal Error creating SMS Client object", Err.Number, Err.Description, Err.Source)
ElseIf (objMSXML3 Is Nothing) Then
Call subCloseApp("Fatal Error creating XML object", Err.Number, Err.Description, Err.Source)
End If
intIPRange = GaryIPRange(IP_END) - GaryIPRange(IP_START)
intOnLine = 0
With objRegularExpression
.Pattern = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
.IgnoreCase = True
.Multiline = True
.Global = True
End With
Set colIPAddresses = objRegularExpression.Execute(strAllIPs)
intItems = colIPAddresses.Count
For intLoop = 0 To intItems - 1
strMessage = "" : strTitle = "" : strTemp = "" : strURL = "" : strPage = ""
lFoundIt = False
Err.Clear
Erase aryPCs
If GlShortFormat Then ReDim aryPCs(5) Else ReDim aryPCs(26)
aryPCs(GintPC_IP) = colIPAddresses.Item(intLoop).Value
intOnLine = intOnLine + 1
intPercentage = CInt((intOnLine / (intIPRange + 1)) * 100)
GobjIE.Document.ParentWindow.Document.Script.BarOP(intPercentage)
If GlAskForInput Then
strResultLine = "<br />" & aryPCs(GintPC_IP) & LINE_HEADER & "Searching..."
strResetLine = funUpdateWindow(intPercentage, strResultLine, strResetLine)
End If
If funConnectable(aryPCs(GintPC_IP), 1, 250) Then
Dim objWMI : Set objWMI = Nothing
On Error Resume Next
Err.Clear
Set objWMI = objLocator.ConnectServer(aryPCs(GintPC_IP), "root\cimv2",,,,, MAX_WAIT)
On Error Goto 0
If (objWMI Is Nothing) Then
Err.Clear
If GlAskForInput Then
strResultLine = LINE_HEADER & "Checking Network..."
GobjIE.Document.ParentWindow.Document.Script.ListOP(strResultLine)
End If
aryPCs(GintPC_ROLE) = "Network Device"
strURL = "http://" & aryPCs(GintPC_IP)
On Error Resume Next
Err.Clear
objMSXML3.open "GET", strURL, False
objMSXML3.setRequestHeader "User-Agent","My funky browser."
objMSXML3.send ""
If objMSXML3.readyState <> 4 Then objMSXML3.waitForResponse 5
On Error Goto 0
If Err.Number = 0 And _
objMSXML3.readyState <> 1 Then
strPage = LCase(objMSXML3.responseText)
If Err.Number = 0 Then
lFoundIt = True
If funCheckILo(strPage, aryPCs) Then
strMessage = "ILo Found"
ElseIf funCheckHP(strPage, aryPCs) Then
strMessage = "HP Device Found"
ElseIf funCheckAPC(strPage, aryPCs) Then
strMessage = "APC Device Found"
Else
strMessage = "Unknown EWS!"
aryPCs(GintPC_Name) = strMessage
End If
ElseIf Err.Number = 424 Then
aryPCs(GintPC_Name) = "Unknown EWS!"
strMessage = "Access Denied!"
aryPCs(GintPC_Model) = strMessage
End If
ElseIf Err.Number = ACCESS_DENIED Then
aryPCs(GintPC_Name) = "Unknown EWS!"
strMessage = "Access Denied!"
aryPCs(GintPC_Model) = strMessage
Else
strMessage = "Nothing found."
End If
strResultLine = LINE_HEADER & strMessage
Else
If GlAskForInput Then
strResultLine = LINE_HEADER & "Connected..."
GobjIE.Document.ParentWindow.Document.Script.ListOP(strResultLine)
End If
lFoundIt = True
Call subGetHostname( aryPCs, objWMI)
Call subGetRoleUser( aryPCs, objWMI)
Call subGetSerialMakeModel(aryPCs, objWMI)
If Not GlShortFormat Then
Call subGetRAM( aryPCs, objWMI)
Call subGetDateOS( aryPCs, objWMI)
Call subGetBIOS( aryPCs, objWMI)
Call subGetCPUSpeed( aryPCs, objWMI)
Call subGetNICsInfo( aryPCs, objWMI)
Call subGetDiskInfo( aryPCs, objWMI)
Call subGetLocalAdmins(aryPCs)
aryPCs(GintPC_SMS) = objSMSClient.AutoDiscoverSite
End If
strResultLine = LINE_HEADER & aryPCs(GintPC_Role) & " Processed."
End If
Else
strMessage = "Ping failed."
strResultLine = LINE_HEADER & strMessage
End If
If lFoundIt Then Call subAddLineXLS(aryPCs)
If GlAskForInput Then GobjIE.Document.ParentWindow.Document.Script.ListOP(strResultLine)
Set objWMI = Nothing
Next
Set colItems = Nothing : Set objMSXML3 = Nothing : Set objSMSClient = Nothing : Set objItem = Nothing
End Sub
Sub subGetHostname(ByRef aryPCs, ByRef objWMI)
Dim colItems : Set colItems = Nothing
Dim objItem : Set objItem = Nothing
Set colItems = objWMI.ExecQuery("SELECT DNSHostName, MACAddress" & _
" FROM Win32_NetworkAdapterConfiguration" & _
" WHERE IPEnabled = TRUE", _
"WQL", _
FLAG_RETURN_IMMEDIATELY + FLAG_FORWARD_ONLY)
For Each objItem In colItems
aryPCs(GintPC_Name) = objItem.DNSHostName
aryPCs(GintPC_MAC) = objItem.MACAddress
Next
End Sub
Sub subGetRoleUser(ByRef aryPCs, ByRef objWMI)
Dim colItems : Set colItems = Nothing
Dim objItem : Set objItem = Nothing
Set colItems = objWMI.ExecQuery("SELECT DomainRole, UserName" & _
" FROM Win32_ComputerSystem", _
"WQL", _
FLAG_RETURN_IMMEDIATELY + FLAG_FORWARD_ONLY)
For Each objItem In colItems
Select Case objItem.DomainRole
Case 0
aryPCs(GintPC_Role) = "Standalone Workstation"
Case 1
aryPCs(GintPC_Role) = "Workstation"
Case 2
aryPCs(GintPC_Role) = "Standalone Server"
Case 3
aryPCs(GintPC_Role) = "Server"
Case 4
aryPCs(GintPC_Role) = "Backup DC"
Case 5
aryPCs(GintPC_Role) = "Primary DC"
Case Else
aryPCs(GintPC_Role) = "Unknown System Role"
End Select
aryPCs(GintPC_User) = objItem.UserName
Next
End Sub
Sub subGetSerialMakeModel(ByRef aryPCs, ByRef objWMI)
Dim colItems : Set colItems = Nothing
Dim objItem : Set objItem = Nothing
Set colItems = objWMI.ExecQuery("SELECT IdentifyingNumber, Name, Vendor" & _
" FROM Win32_ComputerSystemProduct", _
"WQL", _
FLAG_RETURN_IMMEDIATELY + FLAG_FORWARD_ONLY)
For Each objItem In colItems
aryPCs(GintPC_Serial) = objItem.IdentifyingNumber
aryPCs(GintPC_Make) = objItem.Vendor
aryPCs(GintPC_Model) = objItem.Name
Next
End Sub
Sub subGetRAM(ByRef aryPCs, ByRef objWMI)
Dim colItems : Set colItems = Nothing
Dim objItem : Set objItem = Nothing
Set colItems = objWMI.ExecQuery("SELECT TotalPhysicalMemory" & _
" FROM Win32_LogicalMemoryConfiguration", _
"WQL", _
FLAG_RETURN_IMMEDIATELY + FLAG_FORWARD_ONLY)
For Each objItem In colItems : aryPCs(GintPC_RAM) = funSizeFormat(objItem.TotalPhysicalMemory, "KB", "") : Next
End Sub
Sub subGetDateOS(ByRef aryPCs, ByRef objWMI)
Dim strTemp
Dim colItems : Set colItems = Nothing
Dim objItem : Set objItem = Nothing
Set colItems = objWMI.ExecQuery("SELECT Caption, CSDVersion, InstallDate" & _
" FROM Win32_OperatingSystem", _
"WQL", _
FLAG_RETURN_IMMEDIATELY + FLAG_FORWARD_ONLY)
For Each objItem In colItems
strTemp = Left(objItem.InstallDate, 8)
aryPCs(GintPC_Date) = Mid(strTemp, 3, 2) & "/" & Right(strTemp, 2) & "/" & Left(objItem.InstallDate, 4)
aryPCs(GintPC_OS) = Trim(objItem.Caption) & Replace(objItem.CSDVersion, "Service Pack ", " (SP ") & ")"
If InStr(aryPCs(GintPC_OS), "Microsoft Windows ") <> 0 Then
aryPCs(GintPC_OS) = Replace(aryPCs(GintPC_OS), "Microsoft Windows ", "")
ElseIf InStr(aryPCs(GintPC_OS), "Microsoft(R) Windows(R) ") <> 0 Then
aryPCs(GintPC_OS) = Replace(aryPCs(GintPC_OS), "Microsoft(R) Windows(R) ", "")
End If
If InStr(aryPCs(GintPC_OS), "Professional") <> 0 Then
aryPCs(GintPC_OS) = Replace(aryPCs(GintPC_OS), "Professional", "PRO")
ElseIf InStr(aryPCs(GintPC_OS), "Standard Edition") <> 0 Then
aryPCs(GintPC_OS) = Replace(aryPCs(GintPC_OS), "Standard Edition", "SE")
End If
Next
End Sub
Sub subGetBIOS(ByRef aryPCs, ByRef objWMI)
Dim colItems : Set colItems = Nothing
Dim objItem : Set objItem = Nothing
Set colItems = objWMI.ExecQuery("SELECT Version" & _
" FROM Win32_BIOS", _
"WQL", _
FLAG_RETURN_IMMEDIATELY + FLAG_FORWARD_ONLY)
For Each objItem In colItems : aryPCs(GintPC_BIOS) = objItem.Version : Next
End Sub
Sub subGetCPUSpeed(ByRef aryPCs, ByRef objWMI)
Const KB = 1024 : Const MB = 1048576 : Const GB = 1073741824
Dim colItems : Set colItems = Nothing
Dim objItem : Set objItem = Nothing
On Error Resume Next
Err.Clear
Set colItems = objWMI.ExecQuery("SELECT Name, MaxClockSpeed, Description" & _
" FROM Win32_Processor", _
"WQL", _
FLAG_RETURN_IMMEDIATELY + FLAG_FORWARD_ONLY)
If Err.Number = 0 Then
For Each objItem In colItems
aryPCs(GintPC_CPU) = Left(objItem.Name, InStr(objItem.Name, " CPU ") - 1)
aryPCs(GintPC_Speed) = FormatNumber(objItem.MaxClockSpeed / KB) & " GHz"
Next
End If
End Sub
Sub subGetNICsInfo(ByRef aryPCs, ByRef objWMI)
Dim intCounter
Dim colItems : Set colItems = Nothing
Dim objItem : Set objItem = Nothing
Set colItems = objWMI.ExecQuery("SELECT Name, AdapterType" & _
" FROM Win32_NetworkAdapter", _
"WQL", _
FLAG_RETURN_IMMEDIATELY + FLAG_FORWARD_ONLY)
intCounter = 0
For Each objItem In colItems
If objItem.AdapterType = "Ethernet 802.3" Then
Select Case intCounter
Case 1
aryPCs(GintPC_NIC_1) = objItem.Name
Case 2
aryPCs(GintPC_NIC_2) = objItem.Name
Case 3
aryPCs(GintPC_NIC_3) = objItem.Name
Case 4
aryPCs(GintPC_NIC_4) = objItem.Name
Case 5
aryPCs(GintPC_NIC_5) = objItem.Name
End Select
intCounter = intCounter + 1
End If
Next
End Sub
Sub subGetDiskInfo(ByRef aryPCs, ByRef objWMI)
Dim intCounter
Dim colItems : Set colItems = Nothing
Dim objItem : Set objItem = Nothing
Set colItems = objWMI.ExecQuery("SELECT DeviceID, Size, FreeSpace" & _
" FROM Win32_LogicalDisk" & _
" WHERE DriveType = '3'", _
"WQL", _
FLAG_RETURN_IMMEDIATELY + FLAG_FORWARD_ONLY)
intCounter = 0
For Each objItem In colItems
Select Case UCase(objItem.DeviceID)
Case "C:"
aryPCs(GintPC_C_Size) = funSizeFormat(objItem.Size , "BY", "GB")
aryPCs(GintPC_C_Free) = funSizeFormat(objItem.FreeSpace, "BY", "GB")
Case "D:"
aryPCs(GintPC_D_Size) = funSizeFormat(objItem.Size , "BY", "GB")
aryPCs(GintPC_D_Free) = funSizeFormat(objItem.FreeSpace, "BY", "GB")
Case "E:"
aryPCs(GintPC_E_Size) = funSizeFormat(objItem.Size , "BY", "GB")
aryPCs(GintPC_E_Free) = funSizeFormat(objItem.FreeSpace, "BY", "GB")
End Select
intCounter = intCounter + 1
If intCounter > 2 Then Exit For
Next
End Sub
Sub subGetLocalAdmins(ByRef aryPCs)
Dim strTemp, strToReturn, strLineHeader
Dim colItems : Set colItems = Nothing
Dim objItem : Set objItem = Nothing
Dim objWinNT : Set objItem = Nothing
On Error Resume Next
Err.Clear
Set objWinNT = GetObject("WinNT://" & aryPCs(GintPC_IP))
On Error Goto 0
If Err.Number <> 0 Then
Call subCloseApp("Run time Error.", Err.Number, Err.Description, Err.Source)
ElseIf (objWinNT Is Nothing) Then
Call subCloseApp("Fatal Error creating Win NT object", Err.Number, Err.Description, Err.Source)
End If
strToReturn = "" : strLineHeader = ""
If Err.Number = 0 And _
Not (objWinNT Is Nothing) Then
On Error Resume Next
Err.Clear
objWinNT.GetInfo
On Error Goto 0
If Err.Number = 0 And _
objWinNT.PropertyCount > 0 Then
On Error Resume Next
Err.Clear
Set colItems = GetObject("WinNT://" & aryPCs(GintPC_IP) & "/Administrators,group")
On Error Goto 0
If Err.Number = 0 And _
Not (objWinNT Is Nothing) And _
colItems.PropertyCount > 0 Then
For Each objItem In colItems.Members
strTemp = Right(objItem.adsPath, Len(objItem.adsPath) - 8)
If InStr(strTemp, "/") <> 0 And _
InStr(strTemp, "$") = 0 And _
InStr(strTemp, " ") = 0 And _
InStr(strTemp, "oa0ad01") = 0 Then
strToReturn = strLineHeader & strTemp
strLineHeader = strToReturn & ", "
End If
Next
End If
End If
End If
aryPCs(GintPC_Admins) = strToReturn
End Sub
Function funSetThingsUp(ByRef strStart)
Dim dteToday, dteNow
Dim strFile
dteToday = Date()
dteNow = Time()
strStart = "Inventory run started: " & dteToday & " at " & dteNow
strFile = Right("0000" & Year(dteToday) , 2) & _
Right("00" & Month(dteToday), 2) & _
Right("00" & Day(dteToday) , 2) & _
"_" & _
Right("00" & Hour(dteNow) , 2) & _
Right("00" & Minute(dteNow) , 2) & REPORT_TITLE
funSetThingsUp = strFile
End Function
Function funIPCreate()
Const IP_BOTTOM = 0 : Const IP_TOP = 255
Const IP_SUBNET = 0 : Const IP_START = 1 : Const IP_END = 2
Dim intCounter
Dim strIPList, strCurrentIP, strLineHeader
Dim colTemp : Set colTemp = Nothing
Dim objRegularExpression : Set objRegularExpression = New RegExp
strIPList = ""
strCurrentIP = funGetIP()
With objRegularExpression
.Pattern = "(\d{1,3}\.\d{1,3}\.\d{1,3})\.\d{1,3}"
.IgnoreCase = True
.Multiline = True
.Global = False
End With
Set colTemp = objRegularExpression.Execute(strCurrentIP)
If colTemp.Count > 0 Then
GaryIPRange(IP_SUBNET) = colTemp.Item(0).Submatches(0)
End If
If GlAskForInput Then
GaryIPRange(IP_SUBNET) = InputBox ("Enter Subnet to Scan - <enter> for Local Subnet", REPORT_TITLE, GaryIPRange(IP_SUBNET))
GaryIPRange(IP_START) = InputBox ("Start at :", "Scanning Subnet: " & GaryIPRange(IP_SUBNET), IP_BOTTOM)
GaryIPRange(IP_END) = InputBox (" End at :", "Scanning Subnet: " & GaryIPRange(IP_SUBNET), IP_TOP)
Else
GaryIPRange(IP_START) = IP_BOTTOM
GaryIPRange(IP_END) = IP_TOP
End If
strLineHeader = ""
For intCounter = GaryIPRange(IP_START) To GaryIPRange(IP_END)
strIPList = strLineHeader & GaryIPRange(IP_SUBNET) & "." & intCounter
strLineHeader = strIPList & vbCrLf
Next
funIPCreate = strIPList
Set colTemp = Nothing : Set objRegularExpression = Nothing
End Function
Function funGetIP()
Const MAX_WAIT = &H80
Dim intCounter
Dim strIPAddress : strIPAddress = "0.0.0.0"
Dim colItems : Set colItems = Nothing
Dim objItem : Set objItem = Nothing
Dim objLocator : Set objLocator = Nothing
Dim objWMI : Set objWMI = Nothing
On Error Resume Next
Err.Clear
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMI = objLocator.ConnectServer(".", "root\cimv2",,,,, MAX_WAIT)
On Error Goto 0
If Err.Number <> 0 Then
Call subCloseApp("Run time Error.", Err.Number, Err.Description, Err.Source)
ElseIf (objLocator Is Nothing) Then
Call subCloseApp("Fatal Error creating Locator object", Err.Number, Err.Description, Err.Source)
ElseIf (objWMI Is Nothing) Then
Call subCloseApp("Fatal Error creating WMI object", Err.Number, Err.Description, Err.Source)
End If
strIPAddress = ""
If Err.Number = 0 And _
Not (objWMI Is Nothing) Then
Set colItems = objWMI.ExecQuery("SELECT * " & _
" FROM Win32_NetworkAdapterConfiguration " & _
" WHERE IPEnabled = TRUE")
For Each objItem in colItems
If Not IsNull(objItem.IPAddress) Then
For intCounter = LBound(objItem.IPAddress) To UBound(objItem.IPAddress)
strIPAddress = objItem.IPAddress(intCounter)
Next
End If
Next
End If
funGetIP = strIPAddress
End Function
Function funConnectable(strHostName, intCount, intTimeOut)
Dim strRunCommand
Dim intReplyTotal, intFailedAttempts, intTestResult
Dim lReplyValue
Dim objShell : Set objShell = Nothing
On Error Resume Next
Err.Clear
Set objShell = CreateObject("WScript.Shell")
On Error Goto 0
If Err.Number <> 0 Then
Call subCloseApp("Run time Error.", Err.Number, Err.Description, Err.Source)
ElseIf (objShell Is Nothing) Then
Call subCloseApp("Fatal Error creating Shell object", Err.Number, Err.Description, Err.Source)
End If
If VarType(strHostName) = vbString Then
If intCount = vbEmpty Then intCount = 2
If intTimeOut = vbEmpty Then intTimeOut = 750
strRunCommand = "%ComSpec% /c " & _
"%SystemRoot%\system32\PING.EXE" & _
" -n " & intCount & _
" -w " & intTimeout & " " & _
strHostName
Do Until (intReplyTotal = 2) Or (intFailedAttempts = 4)
lReplyValue = objShell.Run(strRunCommand, 0, True)
If (lReplyValue = 0) Then intReplyTotal = intReplyTotal + 1 Else intFailedAttempts = intFailedAttempts + 1
Loop
funConnectable = Not (intFailedAttempts = 4)
Else
funConnectable = False
End If
End Function
Function funCheckILo(strToTest, ByRef aryPCs)
Dim colTemp : Set colTemp = Nothing
Dim objRegularExpression : Set objRegularExpression = New RegExp
funCheckILo = False
If InStr(strToTest, "integrated lights") > 0 Then
With objRegularExpression
.Pattern = "servername=[\\""]+([^;\\""]*)"
.IgnoreCase = True
.Multiline = True
.Global = False
End With
Set colTemp = objRegularExpression.Execute(strToTest)
If colTemp.Count > 0 Then
aryPCs(GintPC_Name) = colTemp.Item(0).Submatches(0)
aryPCs(GintPC_MAKE) = "ILo"
End If
funCheckILo = True
End If
End Function
Function funCheckHP(strToTest, ByRef aryPCs)
Dim colTemp : Set colTemp = Nothing
Dim objRegularExpression : Set objRegularExpression = New RegExp
funCheckHP = False
If InStr(strToTest, "hp ") > 0 Then
aryPCs(GintPC_MAKE) = "HP"
aryPCs(GintPC_MODEL) = "Unknown"
If InStr(strToTest, "sender") > 0 Then
aryPCs(GintPC_ROLE) = "Digital Sender"
With objRegularExpression
.Pattern = "9[0-9]00[^a-z]*"
.IgnoreCase = True
.Multiline = True
.Global = False
End With
Set colTemp = objRegularExpression.Execute(strToTest)
If colTemp.Count > 0 Then aryPCs(GintPC_MODEL) = colTemp.Item(0).Value
Else
aryPCs(GintPC_ROLE) = "LaserJet"
Set objRegularExpression = New RegExp
With objRegularExpression
.Pattern = "laserjet (\w+)"
.IgnoreCase = True
.Multiline = True
.Global = False
End With
Set colTemp = objRegularExpression.Execute(strToTest)
If colTemp.Count > 0 Then aryPCs(GintPC_MODEL) = colTemp.Item(0).Submatches(0)
End If
funCheckHP = True
End If
End Function
Function funCheckAPC(strToTest, ByRef aryPCs)
Dim colTemp : Set colTemp = Nothing
Dim objRegularExpression : Set objRegularExpression = New RegExp
funCheckAPC = False
If InStr(strToTest, " apc ") > 0 Then
aryPCs(GintPC_ROLE) = "APC"
aryPCs(GintPC_MAKE) = "UPS"
aryPCs(GintPC_MODEL) = "Unknown"
funCheckAPC = True
End If
End Function
Function funSaveFiles(strFileName)
Dim strFilter, strTitle, strFullName
Dim objFSO : Set objFSO = Nothing
On Error Resume Next
Err.Clear
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
On Error Goto 0
If Err.Number <> 0 Then
Call subCloseApp("Run time Error.", Err.Number, Err.Description, Err.Source)
ElseIf (objFSO Is Nothing) Then
Call subCloseApp("Fatal Error creating FSO object", Err.Number, Err.Description, Err.Source)
End If
funSaveFiles = False
strFilter = "Excel File (*.xls), *.xls"
strTitle = "Save As"
strFullName = DEFAULT_PATH & strFileName & ".xls"
Err.Clear
If Not (objFSO.FolderExists(DEFAULT_PATH)) Then
On Error Resume Next
Err.Clear
objFSO.CreateFolder(DEFAULT_PATH)
On Error Goto 0
End If
If Err.Number <> 0 Or _
Not (objFSO.FolderExists(DEFAULT_PATH)) Then
MsgBox "Could not create the folder: '" & DEFAULT_PATH & "'"
Else
On Error Resume Next
Err.Clear
If (GlAskForInput) Then
strFullName = GobjExcel.GetSaveAsFilename(strFullName, strFilter, 1, strTitle)
End If
If Err.Number <> 0 Then
MsgBox "Could not save Excel File: '" & strFullName & "'"
Else
Err.Clear
GobjExcel.ActiveWorkbook.SaveAs strFullName
If Err.Number = 0 Then funSaveFiles = True
End If
On Error Goto 0
End If
End Function
Function funUpdateWindow(intPercent, strResult, strReset)
On Error Resume Next
Err.Clear
GobjIE.Document.ParentWindow.Document.Script.BarOP(intPercent)
If (GlAskForInput) Then
GobjIE.Document.ParentWindow.Document.Script.ListOP(strResult)
End If
On Error Goto 0
If Err.Number <> 0 Then
Err.Clear
Call subShowBar()
On Error Resume Next
Err.Clear
GobjIE.Document.ParentWindow.Document.Script.BarOP(intPercent)
If (GlAskForInput) Then
GobjIE.Document.ParentWindow.Document.Script.ListOP(strReset)
End If
On Error Goto 0
End If
Do While (GobjIE.Busy)
Sleep 250
Loop
funUpdateWindow = strReset & strResult
End Function
Function funSizeFormat(intBaseNumber, strCurrentSize, strReturnSize)
Const BYTE_VALUE = 0 : Const KB_VALUE = 1 : Const MB_VALUE = 2 : Const GB_VALUE = 3 : Const TB_VALUE = 4
Const KB = 1024 : Const MB = 1048576 : Const GB = 1073741824
Dim intCurrentOffset, intReturnOffset, intTestValue
Dim strSize
intCurrentOffset = -1
intReturnOffset = -1
If IsNumeric(intBaseNumber) Then
If intBaseNumber > 0 Then
intTestValue = intBaseNumber
strSize = strCurrentSize
Select Case strCurrentSize
Case "KB"
intCurrentOffset = KB_VALUE
Case "MB"
intCurrentOffset = MB_VALUE
Case "GB"
intCurrentOffset = GB_VALUE
Case "TB"
intCurrentOffset = TB_VALUE
Case Else
intCurrentOffset = BYTE_VALUE
strSize = "Bytes"
End Select
Select Case strReturnSize
Case "BY"
intReturnOffset = BYTE_VALUE
Case "KB"
intReturnOffset = KB_VALUE
Case "MB"
intReturnOffset = MB_VALUE
Case "GB"
intReturnOffset = GB_VALUE
Case Else
intReturnOffset = TB_VALUE
End Select
Do While strReturnSize <> strSize
If intTestValue < (KB / 2) Then
Exit Do
End If
If intCurrentOffset < intReturnOffset Then
intCurrentOffset = intCurrentOffset + 1
intTestValue = intTestValue / KB
Select Case strSize
Case "Bytes"
strSize = "KB"
Case "KB"
strSize = "MB"
Case "MB"
strSize = "GB"
Case "GB"
strSize = "TB"
Case "TB"
strSize = "Error!"
End Select
Else
intCurrentOffset = intCurrentOffset - 1
intTestValue = intTestValue * KB
Select Case strSize
Case "Bytes"
strSize = "Error!"
Case "KB"
strSize = "Bytes"
Case "MB"
strSize = "KB"
Case "GB"
strSize = "MB"
Case "TB"
strSize = "GB"
End Select
End If
Loop
funSizeFormat = FormatNumber(Round(intTestValue, 2), 2) & " " & strSize
Else
funSizeFormat = ""
End If
Else
funSizeFormat = intBaseNumber
End If
End Function
Points of Interest
This thing took on a life of its own. I learned more about VBScript then I ever wanted to know. Check out the code, there are a lot of jewels I found along the way.
History
Submitted 11/19/2007