VBS to inventory your workstations or servers

May 24, 2016 | Scripting and programming, Server administration, Technology, Windows | 0 comments

Sometimes you just need a clear picture of what is in your environment.

Sometimes you just need to see how much disk space, RAM and CPU capacity a few dozen workstations have available.

Sometimes you walk into an office and you haven’t a clue what’s live, what’s turned off and what is very out of date.

This script fixes all of that. Give it a text file with as many Windows workstations or servers as you like and let it go off and get the version of Windows, the license type, the RAM, CPU, Hard disk capacity, the software and hardware serial number and the Windows installation directory. It saves this into a CSV file that you can then open in Excel to sort and play with as much as you want.

I wrote this to create an inventory of the servers in use here so I could see what version of windows they have, ensure they are using the most recent service pack and ensure they were all using the enterprise skew. I then expanded it to give me more information. Mainly so I can save it somewhere for use on a later date.

On Error Resume Next

Const ForReading = 1
Const ForWriting = 2
Const Forappend = 8
filecomputer = “.\computer.txt” ‘ -> File that contains the computers list
strComputer = “.”
Star = “*”
InventoryOutputFile = “inventory.txt”
logfilename = “.\inventory.log”

Dim hostname,OSType,CSDVersion,WinSerialNumber,Version,Windir,Timewritten,sw,Make,Model,CPU,Drives,TotalMemory,SerialNumber

sw = 0

‘—————————————
‘ Call the routine for log file creation
‘—————————————

createlogfile logfilename

‘————————
‘ Read computer.txt file
‘————————

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objTextFile = objFSO.OpenTextFile(filecomputer,ForReading)
Do While objTextFile.AtEndOfStream <> True
strLinetoParse = objTextFile.ReadLine
First_char = Left(strLinetoParse, 1)
if (First_char <> star) And (First_char <> ” “) then ‘ Bypass the row starting with * and blank char
hostname = Trim(strLinetoParse)

Get_user_info hostname
If WinSerialNumber > 0 then ‘ If the computer exist or is UP
hostname = strLinetoParse

AddLineToOutputFile hostname,OSType,CSDVersion,WinSerialNumber,Version,Windir,TotalMemory,Make,Model,CPU,Drives,SerialNumber,Forappend
WinSerialNumber = 0
Else
msg = “=====> Hostname ” & hostname & ” not found or Down”

writetofile msg,Forappend
End if
End if
Loop
objTextFile.Close

‘————————————————–
‘ Retrieve the information from the remote computer
‘————————————————–

Function Get_user_info(strComputer)
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colItems = objWMIService.ExecQuery(“Select * from Win32_OperatingSystem”,,48)
For Each objItem in colItems
OSType = objItem.Caption
CSDVersion = objItem.CSDVersion
WinSerialNumber = objItem.SerialNumber
Version = objItem.Version
Windir = objItem.WindowsDirectory
TotalMemory = Round(Trim(objItem.TotalVisibleMemorySize) / 1024, 2 /1024) /1024
Next

Set colItems = objWMIService.ExecQuery(“SELECT * FROM Win32_Processor”,,48)
For Each objItem in colItems
CPU = Trim(objItem.Name)
Next

Set colItems = objWMIService.ExecQuery(“SELECT * FROM Win32_LogicalDisk WHERE DriveType=3″,,48)
For Each objItem in colItems
Drives = Drives & Trim(objItem.DeviceID) & ” ” & Round(Trim(objItem.Size) / (1024^2), 2) & “;”
Next

Set colItems = objWMIService.ExecQuery(“SELECT * FROM Win32_ComputerSystem”,,48)
For Each objItem in colItems
Make = Trim(objItem.Manufacturer)
Model = Trim(objItem.Model)
Next

Set colItems = objWMIService.ExecQuery(“SELECT * FROM Win32_BIOS”,,48)
For Each objItem in colItems
SerialNumber = Trim(objItem.SerialNumber)
Next
End function

‘————————
‘ Add/Update the records
‘————————

function AddLineToOutputFile(hostname,OSType,CSDVersion,WinSerialNumber,Version,Windir,TotalMemory,Make,Model,CPU,Drives,SerialNumber,forwriting)
‘WScript.Echo hostname
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set WriteInventory = objFSO.OpenTextFile(InventoryOutputFile,forwriting, True)
WriteInventory.WriteLine(hostname & “,” & OSType & “,” & CSDVersion & “,” & WinSerialNumber & “,” & Version & “,” & Windir & “,” & TotalMemory & “,” & Make& “,” & Model & “,” & CPU & “,” & Drives & “,” & SerialNumber)
WriteInventory.Close
End function

‘———————
‘ Create the log file
‘———————

function createlogfile(logfilename)
Dim objFileSystem, objOutputFile
Dim strOutputFile
strOutputFile = logfilename
Set objFileSystem = CreateObject(“Scripting.fileSystemObject”)
Set objOutputFile = objFileSystem.CreateTextFile(strOutputFile, TRUE)
objOutputFile.WriteLine(“——————— Inventory Report ” & now() & ” ———————“)
objOutputFile.WriteLine(” “)
objOutputFile.Close
Set objFileSystem = Nothing
End function

‘——————-
‘ Write to log file
‘——————-

function writetofile(msg,forwriting)
Set myFSO = CreateObject(“Scripting.FileSystemObject”)
Set WriteStuff = myFSO.OpenTextFile(logfilename,forwriting, True)
WriteStuff.WriteLine(“—————————————————————————“)
WriteStuff.WriteLine(” “)
WriteStuff.WriteLine(msg)
WriteStuff.WriteLine(” “)
WriteStuff.Close
End function

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.