Avatar of Luis Diaz
Luis Diaz
Flag for Colombia asked on

Batch or VB Script : System inventory of PC

Hello experts,

I need to a VB Script or a bat file that do the following:

Write into a system-inventory.xls file (Generated at the same place in which the script is launch) of a computer (not a server)

Manufacturer
Serial number
OS (64 /32 bit)
Processor
C:\ Total (GB)
C:\ Avail (GB)
Memory Total (GB)
Memory Avail (GB)


There is an excellent code which retrieve all this information ID: 40648408, however I would like to launch the same script for the active computer without specifying the computer name or IP address.

Thank you in advance for your help.
VB Script

Avatar of undefined
Last Comment
RobSampson

8/22/2022 - Mon
RobSampson

Hi, taking that solution as a base, it's just a matter of modifying the array to include the local computer, and it generates the same CSV file.

Regards,

Rob.

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objNetwork = CreateObject("WScript.Network")

arrComputers = Array(objNetwork.ComputerName)
strOutputFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "HardwareAudit.csv"

Set objOutput = objFSO.CreateTextFile(strOutputFile, True)
objOutput.WriteLine """Computername"",""IP Address"",""Manufacturer"",""Model"",""Serial Number"",""C:\ Total (GB)"",""C:\ Avail (GB)"",""Memory Total (GB)"",""Memory Avail (GB)"",""Processor"",""Operating System"",""OS Version"",""Service Pack"""

For Each strComputer In arrComputers
	' Create the WMI session
	If Ping(strComputer) = True Then
		Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
		
		' Get computername, manufacturer, and model
		Set colComputer = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
		For Each objComputer in colComputer
		    strHostName = objComputer.Name
		    strManufacturer = objComputer.Manufacturer
		    strModel = objComputer.Model
		Next
		
		'Get IP Address
		Set colComputerIP = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration",,48)
		strIPAddress = ""
		For Each IPConfig in colComputerIP
			If Not IsNull(IPConfig.IPAddress) Then 
				'strIPAddress = strIPAddress & IPConfig.Description & ": "
		        For intIPCount = LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
		        	If IPConfig.IPAddress(intIPCount) <> "0.0.0.0" And InStr(IPConfig.IPAddress(intIPCount), ".") > 0 Then
						If strIPAddress = "" Then
							strIPAddress = IPConfig.IPAddress(intIPCount)
						Else
							strIPAddress = strIPAddress & ";" & IPConfig.IPAddress(intIPCount)
						End If
					End If
				Next
			End If
		Next
	
		' Get the serial number
		Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
		For Each objItem In colItems
			strSerialNumber = Trim(objItem.SerialNumber)
		Next
		
		' Get OS information
		Set colSystemInfo = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
		For Each objItem in colSystemInfo
		     strOS_Caption = objItem.Caption
		     strOS_SPVersion = objItem.CSDVersion
		     strOS_VerNumber = objItem.Version
		     strOS_Memory = Trim(FormatNumber(objItem.TotalVisibleMemorySize/1024,0))
		Next
	
		' Get available memory
		Set colItems = objWMIService.ExecQuery("Select * from Win32_PerfFormattedData_PerfOS_Memory",,48)
		For Each objItem in colItems
			strAvailableMemory = Trim(Round(objItem.AvailableBytes / (1024 *1024 * 1024),2))
		Next
		
		' Get CPU Details
		Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor",,48)
		strCPUDetails = ""
		For Each objItem In colItems
			If strCPUDetails = "" Then
				strCPUDetails = Trim(objItem.Name)
			Else
				strCPUDetails = strCPUDeatils & ";" & Trim(objItem.Name)
			End If
		Next
	
		' Get Hard Drive Size
		Dim strDiskSizes
		Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
		Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = 3 And Name='C:'")
		For Each objItem In colItems
		   	strDiskSize = Trim(Round(objItem.Size / 1024 / 1024 / 1024, 2))
		   	strDiskFreeSpace = Trim(Round(objItem.FreeSpace / 1024 / 1024 / 1024, 2))
		Next
		
		objOutput.WriteLine """" & strHostName & """,""" & strIPAddress & """,""" & strManufacturer & """,""" & strModel & """,""" & strSerialNumber & """,""" & strDiskSize & """,""" & strDiskFreeSpace & """,""" & strOS_Memory & """,""" & strAvailableMemory & """,""" & strCPUDetails & """,""" & strOS_Caption & """,""" & strOS_VerNumber & """,""" & strOS_SPVersion & """"
	End If
Next

objOutput.Close
Set objOutput = Nothing
WScript.Echo "Done. Please see " & strOutputFile

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

Luis Diaz

ASKER
Excellent, Is it possible to add the 32 or 64 bits OS ?
Thank you in advance for your help.
ASKER CERTIFIED SOLUTION
RobSampson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Luis Diaz

ASKER
Excellent! Thank you again Rob!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Luis Diaz

ASKER
Excellent!
RobSampson

No problem.

Rob.