Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag 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.
Avatar of RobSampson
RobSampson
Flag of Australia image

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

Avatar of 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
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent! Thank you again Rob!
Excellent!
No problem.

Rob.