1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
'========================================================================= ' ComputerInventory.vbs ' VERSION: 1.0 ' AUTHOR: Brian Steinmeyer ' EMAIL: sigkill@sigkillit.com ' WEB: http://sigkillit.com ' DATE: 1/21/2013 ' COMPATIBLE: Windows XP+, Server 2003+. If you are using on Windows 2003, ' to get correct CPU Physical and Core Counts make sure you have the following ' hotfix installed: http://support.microsoft.com/kb/932370 ' COMMENTS: Loops through a line delineated list of servers and reports back ' the Manufacturer, Model #, Serial #, Windows OS, OS Architecture (32vs64), ' RAM(MB), CPU Description, CPU Speed(GHz), CPU Physical Count, CPU Core Count, ' Local Volumes with Total and Used Space in GB. ' Edit the configuration to point to a text file containing the names of ' the computers to inventory. By default, it will search the same directory ' of the script, otherwise you can provide the full path. Also, set the max ' number of Disk Volumes you want to return. By default, this is set to 5 but, ' you may need to increase this number if your computers use a lot of volumes. ' EXAMPLE: Inventory Servers.txt in the Current Directory with a max of 5 volumes ' Dim computerList: computerList = "servers.txt" ' Dim maxVolumes: maxVolumes = 5 ' EXAMPLE: Inventory Servers.txt using a full path with a max of 8 volumes ' Dim computerList: computerList = "C:\servers.txt" ' Dim maxVolumes: maxVolumes = 8 '========================================================================= Option Explicit ' ------ SCRIPT CONFIGURATION ------ Dim computerList: computerList = "servers.txt" Dim maxVolumes: maxVolumes = 5 ' ------ END CONFIGURATION ------ 'Ensure Computer List Exists Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject") If Not objFSO.FileExists(computerList) Then Wscript.Echo "ERROR - Input File Does Not Exist!" & vbCrLf & vbCrLf & "Quitting Script!" Wscript.Quit End If 'Create Log File Dim strLogFile: strLogFile = Replace(Wscript.ScriptName, ".vbs", ".txt") Dim strHeadings: strHeadings = "COMPUTER" & vbTab & _ "MAKE" & vbTab & _ "MODEL" & vbTab & _ "SERIAL" & vbTab & _ "OS" & vbTab & _ "ARCHITECTURE" & vbTab & _ "RAM(MB)" & vbTab & _ "CPU" & vbTab & _ "SPEED(GHz)" & vbTab & _ "PHYSICAL" & vbTab & _ "CORE" Dim i Dim strVolumes: strVolumes = "" If maxVolumes > 0 Then For i = 1 to maxVolumes strVolumes = strVolumes & vbTab & "HDD(Used/Total)" Next Else 'Default to Single Volume If an Invalid # Was Assigned maxVolumes = 1 End If strHeadings = strHeadings & strVolumes Call Logger(strLogFile, strHeadings, True) 'Get Server Info Const ForReading = 1 Dim objTextFile: Set objTextFile = objFSO.OpenTextFile(computerList, ForReading) Dim strComp, strManufacturer, strModel, strSerial, strOS, strOSArchitecture, strRAM, strProcessor, strVolume, strTemp Do Until objTextFile.AtEndOfStream strComp = Trim(objTextFile.Readline) If strComp <> "" Then If ServerUpTime(strComp) = "!~ERROR~!" Then strTemp = strComp & "(Offline)" & vbTab & _ "!~ERROR~!" & vbTab & _ "!~ERROR~!" & vbTab & _ "!~ERROR~!" & vbTab & _ "!~ERROR~!" & vbTab & _ "!~ERROR~!" & vbTab & _ "!~ERROR~!" & vbTab & _ "!~ERROR~!" & vbTab & _ "!~ERROR~!" & vbTab & _ "!~ERROR~!" & vbTab & _ "!~ERROR~!" & Replace(strVolumes, "HDD(Used/Total)", "!~ERROR~!") Call Logger(strLogFile, strTemp, False) Else strManufacturer = GetManufacturer(strComp) strModel = GetModel(strComp) strSerial = GetSerial(strComp) strOS = GetOS(strComp) strOSArchitecture = GetOSArchitecture(strComp) strRAM = GetRAM(strComp) strProcessor = GetProcessor(strComp) strVolume = GetVolume(strComp, maxVolumes) Call Logger(strLogFile, strComp & vbTab & strManufacturer & vbTab & strModel & vbTab & strSerial & vbTab & strOS & vbTab & strOSArchitecture & vbTab & strRAM & vbTab & strProcessor & vbTab & strVolume, False) End If End If Loop Wscript.Echo "Finished" Private Function ServerUpTime(strComputer) On Error Resume Next Dim objWMISettings: Set objWMISettings = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Dim colItems: Set colItems = objWMISettings.ExecQuery("Select * from Win32_OperatingSystem") Dim strResult Dim objItem, dtmBootup, dtmLastBootupTime, dtmSystemUptime For Each objItem in colItems dtmBootup = objItem.LastBootUpTime dtmLastBootupTime = CDate(Mid(dtmBootup, 5, 2) & "/" & Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) & " " & Mid (dtmBootup, 9, 2) & ":" & Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, 13, 2)) dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now) Next 'Return Results If Err.Number <> 0 Then Err.Clear strResult = "!~ERROR~!" Else strResult = dtmSystemUptime End If ServerUpTime = strResult 'Kill Objects Set objWMISettings = Nothing On Error GoTo 0 ' End Error Handling End Function Private Function GetManufacturer(strComputer) On Error Resume Next Dim objWMIService: Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Dim colItems: Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem") Dim strResult Dim objItem For Each objItem in colItems strResult = Trim(objItem.Manufacturer) Next 'Return Results If Err.Number <> 0 Then Err.Clear strResult = "!~ERROR~!" End If GetManufacturer = strResult 'Kill Objects Set objWMIService = Nothing On Error Goto 0 End Function Private Function GetModel(strComputer) On Error Resume Next Dim objWMIService: Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Dim colItems: Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem") Dim strResult Dim objItem For Each objItem in colItems strResult = Trim(objItem.Model) Next 'Return Results If Err.Number <> 0 Then Err.Clear strResult = "!~ERROR~!" End If GetModel = strResult 'Kill Objects Set objWMIService = Nothing On Error Goto 0 End Function Private Function GetSerial(strComputer) On Error Resume Next Dim objWMIService: Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Dim colItems: Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS") Dim strResult Dim objItem For Each objItem in colItems strResult = Trim(objItem.SerialNumber) Next 'Return Results If Err.Number <> 0 Then Err.Clear strResult = "!~ERROR~!" End If GetSerial = strResult 'Kill Objects Set objWMIService = Nothing On Error Goto 0 End Function Private Function GetOS(strComputer) On Error Resume Next Dim objWMIService: Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Dim colItems: Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem") Dim strResult Dim objItem For Each objItem in colItems strResult = Trim(objItem.Caption) Next 'Return Results If Err.Number <> 0 Then Err.Clear strResult = "!~ERROR~!" End If GetOS = strResult 'Kill Objects Set objWMIService = Nothing On Error Goto 0 End Function Private Function GetOSArchitecture(strComputer) On Error Resume Next Dim objWMIService: Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" & strComputer & "\root\cimv2") Dim colItems: Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Processor") Dim strResult Dim objItem For Each objItem In colItems Select Case objItem.Architecture Case 0 strResult = "32-bit" Case 9 strResult = "64-bit" End Select Next 'Return Results If Err.Number <> 0 Then Err.Clear strResult = "!~ERROR~!" End If GetOSArchitecture = strResult 'Kill Ovjects Set objWMIService = Nothing On Error Goto 0 End Function Private Function GetRAM(strComputer) On Error Resume Next Dim objWMIService: Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Dim colItems: Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem") Dim strResult Dim objItem For Each objItem in colItems strResult = Round(objItem.TotalPhysicalMemory/1024/1024,0) Next 'Return Results If Err.Number <> 0 Then Err.Clear strResult = "!~ERROR~!" End If GetRAM = strResult 'Kill Objects Set objWMIService = Nothing On Error Goto 0 End Function Private Function GetProcessor(strComputer) 'If Using Server 2003 Install the Following Hotfix to Get Correct Physical and Core CPU Counts 'http://support.microsoft.com/kb/932370 On Error Resume Next Dim objWMIService: Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Dim colItems: Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Processor") Dim strResult Dim objItem Dim strCPU: strCPU = "" Dim strSpeed: strSpeed = "" Dim intPhysical: intPhysical = 0 Dim intCores: intCores = 0 For Each objItem in colItems strCPU = objItem.Description strSpeed = Round(objItem.CurrentClockSpeed/1000,2) intPhysical = colItems.Count intCores = objItem.NumberOfCores Next 'Return Results If Err.Number <> 0 Then Err.clear strResult = "!~ERROR~!" & vbTab & "!~ERROR~!" & vbTab & "!~ERROR~!" & vbTab & "!~ERROR~!" Else strResult = strCPU & vbTab & strSpeed & vbTab & intPhysical & vbTab & intCores End If GetProcessor = strResult 'Kill Objects Set objWMIService = Nothing On Error Goto 0 End Function Private Function GetVolume(strComputer, maxVolCount) On Error Resume Next Dim objWMIService: Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Dim colItems: Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType='3'") Dim strResult: strResult = "" Dim objItem, totalSpace, usedSpace Dim i: i = 0 For Each objItem in colItems totalSpace = FormatNumber(objItem.Size/1024/1024/1024) usedSpace = FormatNumber((objItem.Size - objItem.FreeSpace)/1024/1024/1024) i = i + 1 If i = 1 Then strResult = objItem.DeviceID & "(" & usedSpace & "/" & totalSpace & ")" Elseif i > maxVolCount Then 'Skip Else strResult = strResult & vbTab & objItem.DeviceID & "(" & usedSpace & "/" & totalSpace & ")" End If Next If colItems.Count < maxVolCount Then For i = 1 to maxVolCount - colItems.Count strResult = strResult & vbTab & "N/A" Next End If 'Return Results If Err.Number <> 0 Then Err.Clear If maxVolCount > 0 Then For i = 1 to maxVolCount If i = 1 Then strResult = "!~ERROR~!" Else strResult = strResult & vbTab & "!~ERROR~!" End If Next Else strResult = "!~ERROR~!" End If End If GetVolume = strResult 'Kill Objects Set objWMIService = Nothing On Error Goto 0 End Function Private Sub Logger(fileName, logMessage, blnNewLog) On Error Resume Next Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject") Dim scriptPath: scriptPath = Left(WScript.ScriptFullName,InstrRev(WScript.ScriptFullName,"\")) Dim logName If InStr(1,fileName,"\",1) > 0 Then logName = fileName If objFSO.DriveExists(objFSO.GetDriveName(logName)) Then If StrComp(objFSO.GetExtensionName(logName), "", 1) = 0 Then If Not objFSO.FolderExists(logName) Then If objFSO.FolderExists(objFSO.GetParentFolderName(logName)) Then objFSO.CreateFolder logName 'Create Folder In Current Path Exit Sub Else Call Logger(objFSO.GetParentFolderName(logName), logMessage, blnNewLog) 'Recurse Creating Parent Folder Call Logger(logName, logMessage, blnNewLog) 'Recurse Creating Current Folder Exit Sub End If End If Else If Not objFSO.FileExists(logName) Then If Not objFSO.FolderExists(objFSO.GetParentFolderName(logName)) Then Call Logger(objFSO.GetParentFolderName(logName), logMessage, blnNewLog) 'Recurse Creating Parent Folder Call Logger(logName, logMessage, blnNewLog) 'Recurse Creating Current Folder End If End If End If End If Else logName = scriptPath & fileName End If Dim logFile If blnNewLog = True Then Set logFile = objFSO.CreateTextFile(logName, True) Else If objFSO.FileExists(logName) Then Set logFile = objFSO.OpenTextFile(logName, ForAppending, True) Else Set logFile = objFSO.CreateTextFile(logName, True) End If End If logFile.WriteLine logMessage logFile.Close Set objFSO = Nothing On Error Goto 0 End Sub |