I took the liberty of neatening up your code and fixing a few errors. There's still some work for you to do to get the monitor data into discrete properties.
Function Get-SystemInfo($ComputerName) {
$versionhash = @{
"10.0.17134" = '1803'
"10.0.17763" = '1809'
"10.0.18362" = '1903'
"10.0.18363" = '1909'
"10.0.19041" = '2004'
"10.0.19042" = '2010'
"10.0.20206" = '2101'
}
Foreach ( $computer in $computerName ) {
Write-Progress -Activity "Collecting System Information of $computer"
$OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer
if ($versionhash.ContainsKey($OS.Version)){
$OS.Version = $versionhash.($OS.Version)
}
else{
$OS.Version = "UNKNOWN $($OS.Version)"
}
$sheetS = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computer
If ($null -eq $sheets.UserName) {
$SheetS.UserName = Get-WinEvent -Computer $Computer -FilterHashtable @{Logname = 'Security'; ID = 4672 } -MaxEvents 1 |
Select-Object -ExpandProperty $_.Properties[1].Value
}
$pingStatus = Get-WmiObject -Query "Select * from win32_PingStatus where Address='$Computer'" # Why not use Test-Connection cmdlet?
$cpu = Get-WmiObject -ClassName Win32_ComputerSystem -ComputerName $computer |
Select-Object -Property Name, Manufacturer, Model, UserName
$bios = Get-WmiObject -ClassName Win32_Bios -ComputerName $computer
$MAC = Get-WmiObject win32_networkadapterconfiguration -ComputerName $computer |
Select-Object -Property @{name = 'IPAddress'; Expression = { ($_.IPAddress[0]) } }, MacAddress | Where-Object IPAddress -NE $null
$monitor = Get-WmiObject WmiMonitorID -ComputerName $computer -Namespace root\wmi |
Select-Object @{n = "Manufacturer"; e = { [System.Text.Encoding]::ASCII.GetString($_.ManufacturerName -ne 00) } },
@{n = "Model"; e = { [System.Text.Encoding]::ASCII.GetString($_.UserFriendlyName -ne 00) } },
@{n = "Serial_Number"; e = { [System.Text.Encoding]::ASCII.GetString($_.SerialNumberID -ne 00) } }
Try{
$AD = Get-ADComputer $computer -Properties * -ErrorAction STOP |
Select-Object DistinguishedName, LastLogonDate, OperatingSystemVersion, lastLogonTimestamp, whenChanged, whenCreated, CanonicalName, streetaddress, location
}
Catch{
$AD = "" | Select-Object @{n=DistinguishedName;e={$computer + 'Not found in AD'}}, LastLogonDate, OperatingSystemVersion, lastLogonTimestamp, whenChanged, whenCreated, CanonicalName, streetaddress, location
}
[PSCustomObject]@{
Computer = $computer
Status = $pingStatus
UserName = $sheetS.UserName.trim("\")
OS_Version = $OS.Version
MAC_Address = $MAC.MacAddress
System_Manufacturer = $cpu.Manufacturer
System_Model = $cpu.Model
System_Serial_Number = $bios.SerialNumber
Monitor_1_Manufacturer = $monitor[0]
Monitor_1_Model = $monitor[1]
Monitor_1_Serial = $monitor[2]
Monitor_2_Manufacturer = $monitor[3]
Monitor_2_Model = $monitor[4]
Monitor_2_Serial = $monitor[5]
Monitor_3_Manufacturer = $monitor[6]
Monitor_3_Model = $monitor[7]
Monitor_3_Serial = $monitor[8]
Street_Address = $AD.streetaddress
Location = $AD.location
OU = $AD.CanonicalName
}
}
} # End Function
# how to use function
Get-SystemInfo 'WS06' |
Sort-Object Computer