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.
LVL 1
LD16Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
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

0
LD16Author Commented:
Excellent, Is it possible to add the 32 or 64 bits OS ?
Thank you in advance for your help.
0
RobSampsonCommented:
Sure, I have added (x86) or (x64) after the OS name.

If you want to add this to the other script, change
		' 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

Open in new window


to this
		' Get OS information
		Set colSystemInfo = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
		For Each objItem in colSystemInfo
		     intAddressWidth = objItem.OSArchitecture
		     If intAddressWidth = "32-bit" Then
		     	strOS_Caption = Trim(objItem.Caption) & " (x86)"
		     Else
		     	strOS_Caption = Trim(objItem.Caption) & " (x64)"
		     End If
		     strOS_SPVersion = objItem.CSDVersion
		     strOS_VerNumber = objItem.Version
		     strOS_Memory = Trim(FormatNumber(objItem.TotalVisibleMemorySize/1024,0))
		Next

Open in new window


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
		     intAddressWidth = objItem.OSArchitecture
		     If intAddressWidth = "32-bit" Then
		     	strOS_Caption = Trim(objItem.Caption) & " (x86)"
		     Else
		     	strOS_Caption = Trim(objItem.Caption) & " (x64)"
		     End If
		     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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

LD16Author Commented:
Excellent! Thank you again Rob!
0
LD16Author Commented:
Excellent!
0
RobSampsonCommented:
No problem.

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.