Use PowerShell Script for Inventorying Computers on a Domain.

This PowerShell script is a useful tool for Small and Medium Businesses (SMBs) looking to inventory their computer hardware and monitor information without any hardware asset management tool such as PDQ deploy, ManageEngine EndPoint Central (previously Desktop Central) or SCCM. It utilizes the Windows Management Instrumentation (WMI) technology to remotely query this information from multiple computers, making it a convenient and efficient solution for gathering and organizing this data.

The script begins by defining the input file path and error file path and setting the hostname as the ‘Hostnames’ column in a CSV file. It also creates a function called ‘ConvertTo-Char’ which takes an array as input and converts it to a string. This function is used later in the script to convert the manufacturer name, user-friendly name, and a serial number of the monitors to a string format.

Next, the script checks to see if the hostname is an array or not. If it is not an array, it proceeds to gather data from the specified hostname using WMI. It queries the Win32_BIOS class for the serial number of the computer’s system, the Win32_ComputerSystem class for the manufacturer and model of the computer, and the win32_networkadapter class for the MAC address of the computer. It also queries the WMIMonitorID namespace for information about the monitors connected to the computer.

The script then stores the gathered information in variables, including the primary and secondary monitor’s manufacturer, model, and serial number. It uses the ‘ConvertTo-Char’ function to convert these values to a string format before storing them in the variables. The script then appends this information to a ‘results.csv’ file, including the hostname, serial number, manufacturer, model, primary monitor manufacturer and model, primary monitor serial number, secondary monitor manufacturer and model, secondary monitor serial number, and MAC address.

If the hostname is an array, the script enters a loop that iterates through each hostname in the array and performs the same actions as described above for each hostname. This allows the script to gather information from multiple computers in a single run, saving time and effort for the user.

This script is a valuable tool for SMBs looking to efficiently gather and organize hardware information about their computers, including details about the monitors. The use of WMI allows for the script to be run remotely on multiple computers, and the saved information can be easily accessed and organized in the ‘results.csv’ file. This information can be useful for a variety of purposes, such as tracking computer and monitor inventory, identifying outdated or faulty hardware, and planning for hardware upgrades or replacements. Overall, the PowerShell script provided is a useful and convenient solution for SMBs looking to efficiently gather and organize computer hardware and monitor information.

Find it in my GitHub repo below with updated modifications.

https://github.com/Rhsameera/Sam-s_PS_Inventory/

#---------------Sam's Inventory Script----- V.1.4-----------------#


$InputFilePath=".\host.csv"
$ErrorFilePath=".\"
$Hostname = Import-CSV $InputFilePath | %{$_.'Hostnames'}
$Hostname_type = $Hostname.GetType()
echo "Host Name`tSerial`tManufacturer`tModel`tPri. Monior Manuf.`tPri. Moni. Model`tPri Moni. Serial`tSec. Monitor Manuf.`tSec. Moni. Model`tSec. Moni. Serial`tMac Addr." >> results.csv
Function ConvertTo-Char
(	
	$Array
)
{
	$Output = ""
	ForEach($char in $Array)
	{	$Output += [char]$char -join ""
	}
	return $Output
}

if($Hostname_type.IsArray -eq $false){
        Write-Host -ForegroundColor DarkYellow "Gathering Data From " $Hostname"..." -NoNewline
        try{  
                                       
                            $SreipedSytemSer = Get-WMIObject -Class Win32_BIOS  -ComputerName $Hostname  | select -ExpandProperty SerialNumber
                            $StripedSysManu = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Hostname | select -ExpandProperty Manufacturer
                            $StripedSysModel = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Hostname | select -ExpandProperty Model
                            $SripedMAC = get-wmiobject win32_networkadapter -filter "netconnectionstatus = 2" -ComputerName $Hostname | select -ExpandProperty macaddress
                            $Monitors = Get-WmiObject -Query "Select * FROM WMIMonitorID" -Namespace root\wmi -ComputerName $Hostname
                            
                            $Results = ForEach ($Monitor in $Monitors){    
	                            New-Object PSObject -Property @{
	                            	ComputerName = $CName
	                            	Active = $Monitor.Active
	                            	Manufacturer = ConvertTo-Char($Monitor.ManufacturerName)
	                            	UserFriendlyName = ConvertTo-Char($Monitor.userfriendlyname)
	                            	SerialNumber = ConvertTo-Char($Monitor.serialnumberid)
                                    }
                            }


                            $StripedPriManu = $Results[0] | select -ExpandProperty Manufacturer
                            $StripedPriModel = $Results[0] | select -ExpandProperty UserFriendlyName
                            $StripedPriMonSerial = $Results[0] | select -ExpandProperty SerialNumber
                            
                            $StripedSecManu = $Results[1] | select -ExpandProperty Manufacturer
                            $StripedSecModel = $Results[1] | select -ExpandProperty UserFriendlyName
                            $StripedSecMonSerial = $Results[1] | select -ExpandProperty SerialNumber
                            $CSVHostname = $Hostname


                            echo "$CSVHostname`t$SreipedSytemSer`t$StripedSysManu`t$StripedSysModel`t$StripedPriManu`t$StripedPriModel`t$StripedPriMonSerial`t$StripedSecManu`t$StripedSecModel`t$StripedSecMonSerial`t$SripedMAC" >> results.csv



                            Write-Host -ForegroundColor DarkYellow `r`n$SreipedSytemSer 
                            Write-Host -ForegroundColor DarkYellow "--------------------------------------"
                            
                }catch{
                    Write-Host -ForegroundColor Red "Error while connecting or gathering data on " $Hostname[$i]
                   Add-Content error.txt $Hostname
                }
}else{
            for($i=0;$i -lt $Hostname.Length;$i++){
                Write-Host -ForegroundColor DarkYellow "Gathering Data From " $Hostname[$i] "..." -NoNewline
                           $connectivityavailability = Test-Connection -Count 1 -ComputerName $Hostname[$i]  -Quiet
                           
                           if ($connectivityavailability -eq $True) {
                                                               
                                     try{  
                                                    
                                         $SreipedSytemSer = Get-WMIObject -Class Win32_BIOS  -ComputerName $Hostname[$i]  | select -ExpandProperty SerialNumber
                                         $sysdetails = Get-WMIObject -ComputerName $Hostname[$i] -class Win32_ComputerSystem
                                         $StripedSysManu = $sysdetails.Manufacturer
                                         $StripedSysModel = $sysdetails.Model
                                         $SripedMAC = get-wmiobject win32_networkadapter -filter "netconnectionstatus = 2" -ComputerName $Hostname[$i] | select -ExpandProperty macaddress
                                         $Monitors = Get-WmiObject -Query "Select * FROM WMIMonitorID" -Namespace root\wmi -ComputerName $Hostname[$i]
                                         
                                         $Results = ForEach ($Monitor in $Monitors){    
	                                         New-Object PSObject -Property @{
	                                         	ComputerName = $CName
	                                         	Active = $Monitor.Active
	                                         	Manufacturer = ConvertTo-Char($Monitor.ManufacturerName)
	                                         	UserFriendlyName = ConvertTo-Char($Monitor.userfriendlyname)
	                                         	SerialNumber = ConvertTo-Char($Monitor.serialnumberid)
                                                 }
                                         }


                                         $StripedPriManu = $Results[0] | select -ExpandProperty Manufacturer
                                         $StripedPriModel = $Results[0] | select -ExpandProperty UserFriendlyName
                                         $StripedPriMonSerial = $Results[0] | select -ExpandProperty SerialNumber
                                         
                                         $StripedSecManu = $Results[1] | select -ExpandProperty Manufacturer
                                         $StripedSecModel = $Results[1] | select -ExpandProperty UserFriendlyName
                                         $StripedSecMonSerial = $Results[1] | select -ExpandProperty SerialNumber
                                         $CSVHostname = $Hostname[$i]


                                         echo "$CSVHostname`t$SreipedSytemSer`t$StripedSysManu`t$StripedSysModel`t$StripedPriManu`t$StripedPriModel`t$StripedPriMonSerial`t$StripedSecManu`t$StripedSecModel`t$StripedSecMonSerial`t$SripedMAC" >> results.csv



                                         Write-Host -ForegroundColor DarkYellow `r`n$SreipedSytemSer 
                                         Write-Host -ForegroundColor DarkYellow "--------------------------------------"
                                         
                                     }catch{
                                         Write-Host -ForegroundColor Red "Error while connecting or gathering data"
                                         $errHostname = $Hostname[$i]
                                         Add-Content error.txt $errHostname

                                     }

                                     }else{
                                     $errHostname = $Hostname[$i]
                                     Write-Host -ForegroundColor Red "Error While Connecting... Cannot ping the host $errHostname"
                                     $errHostname = $Hostname[$i]
                                         Add-Content error.txt $errHostname
                                     }

            }
                
 }
            
        
   





$Results = ForEach ($Monitor in $Query)
{    
	New-Object PSObject -Property @{
		ComputerName = $CName
		Active = $Monitor.Active
		Manufacturer = ConvertTo-Char($Monitor.ManufacturerName)
		UserFriendlyName = ConvertTo-Char($Monitor.userfriendlyname)
		SerialNumber = ConvertTo-Char($Monitor.serialnumberid)

	}
}

$Results | Select ComputerName,Active,Manufacturer,UserFriendlyName,SerialNumber,WeekOfManufacture,YearOfManufacture

Find out more about using PowerShell for VMware automation in other posts I made