Inventory script not working properly

Hi,

I have this below script that I put together using various bits I found online, to inventory our PC’s across the site. The script uses a text file populated with the names of the PC’s and outputs the info in a csv. This was working fine until I added the bits to get monitor info and installed/Pending Updates.

Now it works on some PC’s but not on others. I don’t think it likes PC’s with dual monitors. Is there a way to make that work?

I get errors like this a lot now.

Cannot convert the “System.Object” value of type “System.Object” to type “System.Management.ManagementObject”.

Any help getting this working on all the PC’s would be really appreciated.

#Test if the text file exisits
If (Test-Path C:\PC_ServerList.txt)
{  
     Write-Host "Importing PC/Server hostnames" -ForegroundColor Yellow
}

Else
{
    Write-Host "Cannot find PC_ServerList.txt text file. Please check if the file exists." -ForegroundColor Red
    
Continue
}

#Gets the list of PC names
$ComputerList = Get-Content C:\PC_ServerList.txt


#Or $Computerlist = Get-ADComputer -Filter  {OperatingSystem -Like '*Windows Server*' }  | Select-Object -ExpandProperty Name

$OutputCSV = "C:\PC_ServerInventory.csv"
 
$ScriptPath = $MyInvocation.MyCommand.Path
#$Dir = Split-Path $ScriptPath
Pushd $Dir
 
[System.Collections.ArrayList]$SysCollection = New-Object System.Collections.ArrayList($Null) -ErrorAction SilentlyContinue
  
ForEach ($Computer in $ComputerList)
{
    "Collecting information from $Computer"
  
    Try
    {

        #Starts the necessary services
        Get-Service -Name WinRM -ComputerName $Computer -ErrorAction SilentlyContinue | Set-Service -Startuptype Automatic -Status Running
        Get-Service -Name RemoteRegistry -ComputerName $Computer -ErrorAction SilentlyContinue | Set-Service -Startuptype Automatic -Status Running
        Get-Service -Name COMSysApp -ComputerName $Computer -ErrorAction SilentlyContinue | Set-Service -Startuptype Automatic -Status Running
        Get-Service -Name EventSystem -ComputerName $Computer -ErrorAction SilentlyContinue | Set-Service -Startuptype Automatic -Status Running


        [WMI]$SysInfo     = Get-WmiObject Win32_ComputerSystem -Namespace "Root\CIMV2" -ComputerName $Computer -ErrorAction SilentlyContinue
        [WMI]$Monitors    = Get-WmiObject WmiMonitorID -Namespace "Root\WMI" -ComputerName $Computer -ErrorAction SilentlyContinue
        [WMI]$BIOS        = Get-WmiObject Win32_BIOS -Namespace "Root\CIMV2" -ComputerName $Computer -ErrorAction SilentlyContinue
        [WMI]$OS          = Get-WmiObject Win32_OperatingSystem -Namespace "Root\CIMV2" -ComputerName $Computer -ErrorAction SilentlyContinue
        [WMI]$Processor   = Get-WmiObject Win32_Processor -Namespace "Root\CIMV2" -ComputerName $Computer -ErrorAction SilentlyContinue
        [WMI]$AV          = Get-WmiObject AntiVirusProduct -Namespace "Root\SecurityCenter2" -ComputerName $Computer -ErrorAction SilentlyContinue
        [WMI]$LastUser    = Get-WmiObject Win32_Process -Namespace "Root\CIMV2" -ComputerName $Computer -ErrorAction SilentlyContinue | Where-Object Name -Match Explorer
        [WMI]$TimeZone    = Get-WmiObject Win32_TimeZone -Namespace "Root\CIMV2" -ComputerName $Computer -ErrorAction SilentlyContinue
        [Array]$Disks     = Get-WmiObject Win32_LogicalDisk -Namespace "Root\CIMV2" -ComputerName $Computer -Filter DriveType=3 -ErrorAction SilentlyContinue
        [Array]$USBDisks  = Get-WmiObject Win32_LogicalDisk -Namespace "Root\CIMV2" -ComputerName $Computer -Filter DriveType=2 -ErrorAction SilentlyContinue
        [Array]$NIC       = Get-WmiObject Win32_NetworkAdapterConfiguration -Namespace "Root\CIMV2" -ComputerName $Computer -Filter 'IPEnabled = "True"' -ErrorAction SilentlyContinue

        #Selects the Administrator 
        $LocalAdmin       = Invoke-Command -ComputerName $Computer -ErrorAction SilentlyContinue {Get-LocalUser -Name "Administrator"}

        #Gets the AV Last updated time/date
        $AVDU             = Invoke-Command -ComputerName $Computer -ErrorAction SilentlyContinue {Get-MpComputerStatus | Select-Object -Property AntivirusSignatureLastUpdated}

        #Gets the release ID Number
        $OSVersion        = Invoke-command -ComputerName $Computer -ErrorAction SilentlyContinue {Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"}

        #Gets the system uptime
        $LastBoot         = [System.Management.ManagementDateTimeConverter]::ToDateTime("$($OS.LastBootUpTime)")
        $UpTime           = (Get-Date) - $LastBoot

        #Switch for the AV Real Time Protection and Definition Status
         Switch ($AV.ProductState){
        "262144" {$DefStatus = "Up to date" ;$RTStatus = "Disabled"} 
        "262160" {$DefStatus = "Out of date" ;$RTStatus = "Disabled"} 
        "266240" {$DefStatus = "Up to date" ;$RTStatus = "Enabled"} 
        "266256" {$DefStatus = "Out of date" ;$RTStatus = "Enabled"} 
        "393216" {$DefStatus = "Up to date" ;$RTStatus = "Disabled"} 
        "393232" {$DefStatus = "Out of date" ;$RTStatus = "Disabled"} 
        "393488" {$DefStatus = "Out of date" ;$RTStatus = "Disabled"} 
        "397312" {$DefStatus = "Up to date" ;$RTStatus = "Enabled"} 
        "397328" {$DefStatus = "Out of date" ;$RTStatus = "Enabled"} 
        "397584" {$DefStatus = "Out of date" ;$RTStatus = "Enabled"}
        "397568" {$DefStatus = "Up to date"; $RTStatus = "Enabled"}
        "393472" {$DefStatus = "Up to date" ;$RTStatus = "Disabled"} 
         Default {$DefStatus = "Unknown" ;$RTStatus = "Unknown"}}

         #Switch to list the name of the OS Language
         Switch ($OS.OSLanguage){
        "1078"  {$OSLan = "Afrikaans - South Africa"}
        "1052"  {$OSLan = "Albanian - Albania"}
        "1118"  {$OSLan = "Amharic - Ethiopia"}
        "1025"  {$OSLan = "Arabic - Saudi Arabia"}
        "5121"  {$OSLan = "Arabic - Algeria"}
        "15361" {$OSLan = "Arabic - Bahrain"}
        "3073"  {$OSLan = "Arabic - Egypt"}
        "2049"  {$OSLan = "Arabic - Iraq"}
        "11265" {$OSLan = "Arabic - Jordan"}
        "13313" {$OSLan = "Arabic - Kuwait"}
        "12289" {$OSLan =  "Arabic - Lebanon"}
        "4097"  {$OSLan = "Arabic - Libya"}
        "6145"  {$OSLan = "Arabic - Morocco"}
        "8193"  {$OSLan = "Arabic - Oman"}
        "16385" {$OSLan = "Arabic - Qatar"}
        "10241" {$OSLan = "Arabic - Syria"}
        "7169"  {$OSLan = "Arabic - Tunisia"}
        "14337" {$OSLan = "Arabic - U.A.E."}
        "9217"  {$OSLan = "Arabic - Yemen"}
        "1067"  {$OSLan = "Armenian - Armenia"}
        "1101"  {$OSLan = "Assamese"}
        "2092"  {$OSLan = "Azeri (Cyrillic)"}
        "1068"  {$OSLan = "Azeri (Latin)"}
        "1069"  {$OSLan = "Basque"}
        "1059"  {$OSLan = "Belarusian"}
        "1093"  {$OSLan = "Bengali (India)"}
        "2117"  {$OSLan = "Bengali (Bangladesh)"}
        "5146"  {$OSLan = "Bosnian (Bosnia/Herzegovina)"}
        "1026"  {$OSLan = "Bulgarian"}
        "1109"  {$OSLan = "Burmese"}
        "1027"  {$OSLan = "Catalan"}
        "1116"  {$OSLan = "Cherokee - United States"}
        "2052"  {$OSLan = "Chinese - People's Republic of China"} 
        "4100"  {$OSLan = "Chinese - Singapore"}
        "1028"  {$OSLan = "Chinese - Taiwan"}
        "3076"  {$OSLan = "Chinese - Hong Kong SAR"}
        "5124"  {$OSLan = "Chinese - Macao SAR"}
        "1050"  {$OSLan = "Croatian"}
        "4122"  {$OSLan = "Croatian (Bosnia/Herzegovina)"}
        "1029"  {$OSLan = "Czech"}
        "1030"  {$OSLan = "Danish"}
        "1125"  {$OSLan = "Divehi"}
        "1043"  {$OSLan = "Dutch - Netherlands"}
        "2067"  {$OSLan = "Dutch - Belgium"}
        "1126"  {$OSLan = "Edo"}
        "1033"  {$OSLan = "English - United States"}
        "2057"  {$OSLan = "English - United Kingdom"}
        "3081"  {$OSLan = "English - Australia"}
        "10249" {$OSLan = "English - Belize"}
        "4105"  {$OSLan = "English - Canada"}
        "9225"  {$OSLan = "English - Caribbean"}
        "15369" {$OSLan = "English - Hong Kong SAR"}
        "16393" {$OSLan = "English - India"}
        "14345" {$OSLan = "English - Indonesia"}
        "6153"  {$OSLan = "English - Ireland"}
        "8201"  {$OSLan = "English - Jamaica"}
        "17417" {$OSLan = "English - Malaysia"}
        "5129"  {$OSLan = "English - New Zealand"}
        "13321" {$OSLan = "English - Philippines"}
        "18441" {$OSLan = "English - Singapore"}
        "7177"  {$OSLan = "English - South Africa"}
        "11273" {$OSLan = "English - Trinidad"}
        "12297" {$OSLan = "English - Zimbabwe"}
        "1061"  {$OSLan = "Estonian"}
        "1080"  {$OSLan = "Faroese"}
        "1065"  {$OSLan = "Farsi"}
        "1124"  {$OSLan = "Filipino"}
        "1035"  {$OSLan = "Finnish"}
        "1036"  {$OSLan = "French - France"}
        "2060"  {$OSLan = "French - Belgium"}
        "11276" {$OSLan = "French - Cameroon"}
        "3084"  {$OSLan = "French - Canada"}
        "9228"  {$OSLan = "French - Democratic Rep. of Congo"}
        "12300" {$OSLan = "French - Cote d'Ivoire"}
        "15372" {$OSLan = "French - Haiti"}
        "5132"  {$OSLan = "French - Luxembourg "}
        "13324" {$OSLan = "French - Mali"}
        "6156"  {$OSLan = "French - Monaco"}
        "14348" {$OSLan = "French - Morocco"}
        "58380" {$OSLan = "French - North Africa"}
        "8204"  {$OSLan = "French - Reunion"}
        "10252" {$OSLan = "French - Senegal"}
        "4108"  {$OSLan = "French - Switzerland"}
        "7180"  {$OSLan = "French - West Indies"}
        "1122"  {$OSLan = "Frisian - Netherlands"}
        "1127"  {$OSLan = "Fulfulde - Nigeria"}
        "1071"  {$OSLan = "FYRO Macedonian"}
        "2108"  {$OSLan = "Gaelic (Ireland)"}
        "1084"  {$OSLan = "Gaelic (Scotland)"}
        "1110"  {$OSLan = "Galician"}
        "1079"  {$OSLan = "Georgian"}
        "1031"  {$OSLan = "German - Germany"}
        "3079"  {$OSLan = "German - Austria"}
        "5127"  {$OSLan = "German - Liechtenstein"}
        "4103"  {$OSLan = "German - Luxembourg"}
        "2055"  {$OSLan = "German - Switzerland"}
        "1032"  {$OSLan = "Greek"}
        "1140"  {$OSLan = "Guarani - Paraguay"}
        "1095"  {$OSLan = "Gujarati"}
        "1128"  {$OSLan = "Hausa - Nigeria"}
        "1141"  {$OSLan = "Hawaiian - United States"}
        "1037"  {$OSLan = "Hebrew"}
        "1081"  {$OSLan = "Hindi"}
        "1038"  {$OSLan =" Hungarian"}
        "1129"  {$OSLan = "Ibibio - Nigeria"}
        "1039"  {$OSLan = "Icelandic"}
        "1136"  {$OSLan = "Igbo - Nigeria"}
        "1057"  {$OSLan = "Indonesian"}
        "1117"  {$OSLan = "Inuktitut"}
        "1040"  {$OSLan = "Italian - Italy"}
        "2064"  {$OSLan = "Italian - Switzerland"}
        "1041"  {$OSLan = "Japanese"}
        "1099"  {$OSLan = "Kannada"}
        "1137"  {$OSLan = "Kanuri - Nigeria"}
        "2144"  {$OSLan = "Kashmiri"}
        "1120"  {$OSLan = "Kashmiri (Arabic)"}
        "1087"  {$OSLan = "Kazakh"}
        "1107"  {$OSLan = "Khmer"}
        "1111"  {$OSLan = "Konkani"}
        "1042"  {$OSLan = "Korean"}
        "1088"  {$OSLan = "Kyrgyz (Cyrillic)"}
        "1108"  {$OSLan = "Lao"}
        "1142"  {$OSLan = "Latin"}
        "1062"  {$OSLan = "Latvian"}
        "1063"  {$OSLan = "Lithuanian"}
        "1086"  {$OSLan = "Malay - Malaysia"}
        "2110"  {$OSLan = "Malay - Brunei Darussalam"}
        "1100"  {$OSLan = "Malayalam"}
        "1082"  {$OSLan = "Maltese"}
        "1112"  {$OSLan = "Manipuri"}
        "1153"  {$OSLan = "Maori - New Zealand"}
        "1102"  {$OSLan = "Marathi"}
        "1104"  {$OSLan = "Mongolian (Cyrillic)"}
        "2128"  {$OSLan = "Mongolian (Mongolian)"}
        "1121"  {$OSLan = "Nepali"}
        "2145"  {$OSLan = "Nepali - India"}
        "1044"  {$OSLan = "Norwegian (Bokmål)"}
        "2068"  {$OSLan = "Norwegian (Nynorsk)"}
        "1096"  {$OSLan = "Oriya"}
        "1138"  {$OSLan = "Oromo"}
        "1145"  {$OSLan = "Papiamentu"}
        "1123"  {$OSLan = "Pashto"}
        "1045"  {$OSLan = "Polish"}
        "1046"  {$OSLan = "Portuguese - Brazil"}
        "2070"  {$OSLan = "Portuguese - Portugal"}
        "1094"  {$OSLan = "Punjabi"}
        "2118"  {$OSLan = "Punjabi (Pakistan)"}
        "1131"  {$OSLan = "Quecha - Bolivia"}
        "2155"  {$OSLan = "Quecha - Ecuador"}
        "3179"  {$OSLan = "Quecha - Peru"}
        "1047"  {$OSLan = "Rhaeto-Romanic"}
        "1048"  {$OSLan = "Romanian"}
        "2072"  {$OSLan = "Romanian - Moldava"}
        "1049"  {$OSLan = "Russian"}
        "2073"  {$OSLan = "Russian - Moldava"}
        "1083"  {$OSLan = "Sami (Lappish)"}
        "1103"  {$OSLan = "Sanskrit"}
        "1132"  {$OSLan = "Sepedi"}
        "3098"  {$OSLan = "Serbian (Cyrillic)"}
        "2074"  {$OSLan = "Serbian (Latin)"}
        "1113"  {$OSLan = "Sindhi - India"}
        "2137"  {$OSLan = "Sindhi - Pakistan"}
        "1115"  {$OSLan = "Sinhalese - Sri Lanka"}
        "1051"  {$OSLan = "Slovak"}
        "1060"  {$OSLan = "Slovenian"}
        "1143"  {$OSLan = "Somali"}
        "1070"  {$OSLan = "Sorbian"}
        "3082"  {$OSLan = "Spanish - Spain (Modern Sort)"}
        "1034"  {$OSLan = "Spanish - Spain (Traditional Sort)"}
        "11274" {$OSLan = "Spanish - Argentina"}
        "16394" {$OSLan = "Spanish - Bolivia"}
        "13322" {$OSLan = "Spanish - Chile"}
        "9226"  {$OSLan = "Spanish - Colombia"}
        "5130"  {$OSLan = "Spanish - Costa Rica"}
        "7178"  {$OSLan = "Spanish - Dominican Republic"}
        "12298" {$OSLan = "Spanish - Ecuador"}
        "17418" {$OSLan = "Spanish - El Salvador"}
        "4106"  {$OSLan = "Spanish - Guatemala"}
        "18442" {$OSLan = "Spanish - Honduras"}
        "22538" {$OSLan = "Spanish - Latin America"}
        "2058"  {$OSLan = "Spanish - Mexico"}
        "19466" {$OSLan = "Spanish - Nicaragua"}
        "6154"  {$OSLan = "Spanish - Panama"}
        "15370" {$OSLan = "Spanish - Paraguay"}
        "10250" {$OSLan = "Spanish - Peru"}
        "20490" {$OSLan = "Spanish - Puerto Rico"}
        "21514" {$OSLan = "Spanish - United States"}
        "14346" {$OSLan = "Spanish - Uruguay"}
        "8202"  {$OSLan = "Spanish - Venezuela"}
        "1072"  {$OSLan = "Sutu"}
        "1089"  {$OSLan = "Swahili"}
        "1053"  {$OSLan = "Swedish"}
        "2077"  {$OSLan = "Swedish - Finland"}
        "1114"  {$OSLan = "Syriac"}
        "1064"  {$OSLan = "Tajik"}
        "1119"  {$OSLan = "Tamazight (Arabic)"}
        "2143"  {$OSLan = "Tamazight (Latin)"}
        "1097"  {$OSLan = "Tamil"}
        "1092"  {$OSLan = "Tatar"}
        "1098"  {$OSLan = "Telugu"}
        "1054"  {$OSLan = "Thai"}
        "2129"  {$OSLan = "Tibetan - Bhutan"}
        "1105"  {$OSLan = "Tibetan - People's Rep. of China"}
        "2163"  {$OSLan = "Tigrigna - Eritrea"}
        "1139"  {$OSLan = "Tigrigna - Ethiopia"}
        "1073"  {$OSLan = "Tsonga"}
        "1074"  {$OSLan = "Tswana"}
        "1055"  {$OSLan = "Turkish"}
        "1090"  {$OSLan = "Turkmen"}
        "1152"  {$OSLan = "Uighur - China"}
        "1058"  {$OSLan = "Ukrainian"}
        "1056"  {$OSLan = "Urdu"}
        "2080"  {$OSLan = "Urdu - India"}
        "2115"  {$OSLan = "Uzbek (Cyrillic)"}
        "1091"  {$OSLan = "Uzbek (Latin)"}
        "1075"  {$OSLan = "Venda"}
        "1066"  {$OSLan = "Vietnamese"}
        "1106"  {$OSLan = "Welsh"}
        "1076"  {$OSLan = "Xhosa"}
        "1144"  {$OSLan = "Yi"}
        "1085"  {$OSLan = "Yiddish"}
        "1130"  {$OSLan = "Yoruba"}
        "1077"  {$OSLan = "Zulu"}
        "1279"  {$OSLan = "HID (Human Interface Device)"}}
        
        #Switch to list the name of the monitor manufacturer
        Switch ($MonManufacturer){
        "AAC"   {$MonFriendlyName =	"AcerView"}
        "ACR"   {$MonFriendlyName = "Acer"}
        "AOC"   {$MonFriendlyName = "AOC"}
        "AIC"   {$MonFriendlyName = "AG Neovo"}
        "APP"   {$MonFriendlyName = "Apple Computer"}
        "AST"   {$MonFriendlyName = "AST Research"}
        "AUO"   {$MonFriendlyName = "Asus"}
        "BNQ"   {$MonFriendlyName = "BenQ"}
        "CMO"   {$MonFriendlyName = "Acer"}
        "CPL"   {$MonFriendlyName = "Compal"}
        "CPQ"   {$MonFriendlyName = "Compaq"}
        "CPT"   {$MonFriendlyName = "Chunghwa Pciture Tubes, Ltd."}
        "CTX"   {$MonFriendlyName = "CTX"}
        "DEC"   {$MonFriendlyName = "DEC"}
        "DEL"   {$MonFriendlyName = "Dell"}
        "DPC"   {$MonFriendlyName = "Delta"}
        "DWE"   {$MonFriendlyName = "Daewoo"}
        "EIZ"   {$MonFriendlyName = "EIZO"}
        "ELS"   {$MonFriendlyName = "ELSA"}
        "ENC"   {$MonFriendlyName = "EIZO"}
        "EPI"   {$MonFriendlyName = "Envision"}
        "FCM"   {$MonFriendlyName = "Funai"}
        "FUJ"   {$MonFriendlyName = "Fujitsu"}
        "FUS"   {$MonFriendlyName = "Fujitsu-Siemens"}
        "GSM"   {$MonFriendlyName = "LG Electronics"}
        "GWY"   {$MonFriendlyName = "Gateway 2000"}
        "HEI"   {$MonFriendlyName = "Hyundai"}
        "HIT"   {$MonFriendlyName = "Hyundai"}
	    "HSD"   {$MonFriendlyName = "Hanns-G"}
        "HSL"   {$MonFriendlyName = "Hansol"}
        "HTC"   {$MonFriendlyName = "Hitachi/Nissei"}
        "HWP"   {$MonFriendlyName = "HP"}
        "IBM"   {$MonFriendlyName = "IBM"}
        "ICL"   {$MonFriendlyName = "Fujitsu ICL"}
        "IVM"   {$MonFriendlyName = "Iiyama"}
        "KDS"   {$MonFriendlyName = "Korea Data Systems"}
        "LEN"   {$MonFriendlyName = "Lenovo"}
        "LGD"   {$MonFriendlyName = "Asus"}
        "LPL"   {$MonFriendlyName = "Fujitsu"}
        "MAX"   {$MonFriendlyName = "Belinea"}
        "MEI"   {$MonFriendlyName = "Panasonic"}
        "MEL"   {$MonFriendlyName = "Mitsubishi Electronics"}
        "MS_"   {$MonFriendlyName = "Panasonic"}
        "NAN"   {$MonFriendlyName = "Nanao"}
        "NEC"   {$MonFriendlyName = "NEC"}
        "NOK"   {$MonFriendlyName = "Nokia Data"}
        "NVD"   {$MonFriendlyName = "Fujitsu"}
        "OPT"   {$MonFriendlyName = "Optoma"}
        "PHL"   {$MonFriendlyName = "Philips"}
        "REL"   {$MonFriendlyName = "Relisys"}
        "SAN"   {$MonFriendlyName = "Samsung"}
        "SAM"   {$MonFriendlyName = "Samsung"}
        "SBI"   {$MonFriendlyName = "Smarttech"}
        "SGI"   {$MonFriendlyName = "SGI"}
        "SNY"   {$MonFriendlyName = "Sony"}
        "SRC"   {$MonFriendlyName = "Shamrock"}
        "SUN"   {$MonFriendlyName = "Sun Microsystems"}
        "SEC"   {$MonFriendlyName = "Hewlett-Packard"}
        "TAT"   {$MonFriendlyName = "Tatung"}
        "TOS"   {$MonFriendlyName = "Toshiba"}
        "TSB"   {$MonFriendlyName = "Toshiba"}
        "VSC"   {$MonFriendlyName = "ViewSonic"}
        "ZCM"   {$MonFriendlyName = "Zenith"}
        "UNK"   {$MonFriendlyName = "Unknown"}
        "_YV"   {$MonFriendlyName = "Fujitsu"}
        }

#Get the monitor Model, Serial and Product Number
ForEach ($Monitor in $Monitors)
{

If ([System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName) -NE $Null) {

$MonModel = ([System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName -NE 0)).Replace("$([char]0x0000)","") -join ""

} Else {

$MonModel = $Null

}

$MonSerialNumber = ([System.Text.Encoding]::ASCII.GetString($Monitor.SerialNumberID -NE 0)).Replace("$([char]0x0000)","") -join ""
$MonProductNumber = ([System.Text.Encoding]::ASCII.GetString($Monitor.ProductCodeID -NE 0)).Replace("$([char]0x0000)","") -join ""
$MonManufacturer = ([System.Text.Encoding]::ASCII.GetString($Monitor.ManufacturerName -NE 0)).Replace("$([char]0x0000)","") -join ""

}

#Gets the installed and missing Windows Updates
$UpdateSession = [Activator]::CreateInstance([Type]::GetTypeFromProgID("Microsoft.Update.Session",$Computer))
$UpdateSearcher = $UpdateSession.CreateupdateSearcher()
$MissingUpdates = @($UpdateSearcher.Search("DeploymentAction=* and IsHidden=0 and Type='Software' and IsInstalled=0").Updates)
$InstalledUpdates = @($UpdateSearcher.Search("IsHidden=0 and Type='Software' and IsInstalled=1").Updates)

        
    $SI = @{
            FQDN                          = [String]$SysInfo.DNSHostName+"."+$SysInfo.Domain
            NetBIOSName                   = [String]$SysInfo.DNSHostName
            Domain                        = [String]$SysInfo.Domain
            Manufacturer                  = [String]$SysInfo.Manufacturer
            Model                         = [String]$SysInfo.Model
            SystemType                    = [String]$SysInfo.SystemType
            TotalMemory                   = "$([String]([System.Math]::Round($SysInfo.TotalPhysicalMemory/1GB,2))) GB"
            BIOSName                      = [String]$BIOS.Name
            BIOSDescription               = [String]$BIOS.Description
            BIOSVersion                   = [String]$BIOS.SMBIOSBIOSVersion+"."+$BIOS.SMBIOSMajorVersion+"."+$BIOS.SMBIOSMinorVersion
            BIOSManufacturer              = [String]$BIOS.Manufacturer
            SerialNo                      = [String]$BIOS.SerialNumber
            OSName                        = [String]$OS.Caption
            OSLanguage                    = [String]$OSLan
            OSServicePack                 = (“SP {0}” -F [String]$OS.ServicePackMajorVersion)
            OSArchitecture                = [String]$OS.OSArchitecture
            OSBuildNo                     = [String]$OS.BuildNumber+"."+$OSVersion.UBR
            OSVersion                     = [String]$OS.Version            
            OSReleaseNo                   = [String]$OSVersion.ReleaseID
            OSInstallDate                 = [String]$OS.ConvertToDateTime($OS.InstallDate) -F "MM/dd/yyyy"
            TimeZone                      = [String]$TimeZone.StandardName
            PhysicalProcessors            = [String]@($Processor).Count
            LogicalProcessors             = [String]$Processor.NumberOfLogicalProcessors
            ProcessorModel                = [String]$Processor.Name
            ProcessorManufacturer         = [String]$Processor.Manufacturer
            Cores                         = [String]$Processor.NumberOfCores
            IPAddress                     = [String]$NIC.IPAddress
            MACAddress                    = [String]$NIC.MACAddress
            AVStatus                      = [String]$DefStatus
            RTProtection                  = [String]$RTStatus
            AVLastUpdate                  = [String]$AVDU.AntivirusSignatureLastUpdated
            AntiVirus                     = [String]$AV.DisplayName
            UpTime                        = [String]$UpTime.Days.ToString()+" Days, "+$UpTime.Hours.ToString()+" Hours, "+$UpTime.Minutes.ToString()+" Minutes, "+$UpTime.Seconds.ToString()+" Seconds"
            AdministratorAccountEnabled   = [String]$LocalAdmin.Enabled
            LastUser                      = [String]$LastUser.GetOwner().User
            InstalledWinUpdates           = [String]$InstalledUpdates.Title
            MissingWinUpdates             = [String]$MissingUpdates.Title
            MonitorManufacturer           = [String]$MonFriendlyName
            MonitorModel                  = [String]$MonModel
            MonitorProductNo              = [String]$MonProductNumber
            MonitorSerialNo               = [String]$MonSerialNumber
            }
         
        $Disks    | ForEach-Object {$SI."DriveSize:$($_.Name -Replace ':', '')"="$([String]([System.Math]::Round($_.Size/1GB,2))) GB"} -ErrorAction SilentlyContinue
        $Disks    | ForEach-Object {$SI."FreeDriveSpace:$($_.Name -Replace ':', '')"="$([String]([System.Math]::Round($_.FreeSpace/1GB,2))) GB"} -ErrorAction SilentlyContinue
        $Disks    | ForEach-Object {$SI."UsedDriveSpace:$($_.Name -Replace ':', '')"="$([String]([System.Math]::Round(($_.Size - $_.FreeSpace)/1GB,2))) GB"} -ErrorAction SilentlyContinue
        $USBDisks | ForEach-Object {$SI."DriveSize:$($_.Name -Replace ':', '')"="$([String]([System.Math]::Round($_.Size/1GB,2))) GB"} -ErrorAction SilentlyContinue
        $USBDisks | ForEach-Object {$SI."FreeDriveSpace:$($_.Name -Replace ':', '')"="$([String]([System.Math]::Round($_.FreeSpace/1GB,2))) GB"} -ErrorAction SilentlyContinue
        $USBDisks | ForEach-Object {$SI."UsedDriveSpace:$($_.Name -Replace ':', '')"="$([String]([System.Math]::Round(($_.Size - $_.FreeSpace)/1GB,2))) GB"} -ErrorAction SilentlyContinue
       
    }
    Catch [Exception]
    {
        Write-Host "Error communicating with $Computer, skipping to next" -ForegroundColor White -BackgroundColor Red
        $SI = @{
            Server          = [String]$Computer
            ErrorMessage    = [String]$_.Exception.Message
            ErrorItem       = [String]$Computer
    }
    Continue
}
    Finally
    {
        [Void]$SysCollection.Add((New-Object PSObject -Property $SI))   
    }
}
  
$SysCollection `
    | Select-Object FQDN,NetBIOSName,Domain,Manufacturer,Model,SerialNo,SystemType,ProcessorManufacturer,ProcessorModel,PhysicalProcessors,LogicalProcessors,Cores,TotalMemory,IPAddress,MACAddress,MonitorManufacturer,MonitorModel,MonitorProductNo,MonitorSerialNo,BIOSName,BIOSManufacturer,BiosDescription,BiosVersion,OSName,OSVersion,OSBuildNo,OSReleaseNo,OSArchitecture,OSServicePack,OSLanguage,OSInstallDate,InstalledWinUpdates,MissingWinUpdates,TimeZone,AntiVirus,AVStatus,RTProtection,AVLastUpdate,UpTime,LastUser,AdministratorAccountEnabled,DriveSize:A,DriveSize:B,DriveSize:C,DriveSize:D,DriveSize:E,DriveSize:F,DriveSize:G,DriveSize:H,DriveSize:I,DriveSize:J,DriveSize:K,DriveSize:L,DriveSize:M,DriveSize:N,DriveSize:O,DriveSize:P,DriveSize:Q,DriveSize:R,DriveSize:S,DriveSize:T,DriveSize:U,DriveSize:V,DriveSize:W,DriveSize:X,DriveSize:Y,DriveSize:Z,UsedDriveSpace:A,UsedDriveSpace:B,UsedDriveSpace:C,UsedDriveSpace:D,UsedDriveSpace:E,UsedDriveSpace:F,UsedDriveSpace:G,UsedDriveSpace:H,UsedDriveSpace:I,UsedDriveSpace:J,UsedDriveSpace:K,UsedDriveSpace:L,UsedDriveSpace:M,UsedDriveSpace:N,UsedDriveSpace:O,UsedDriveSpace:P,UsedDriveSpace:Q,UsedDriveSpace:R,UsedDriveSpace:S,UsedDriveSpace:T,UsedDriveSpace:U,UsedDriveSpace:V,UsedDriveSpace:W,UsedDriveSpace:X,UsedDriveSpace:Y,UsedDriveSpace:Z,FreeDriveSpace:A,FreeDriveSpace:B,FreeDriveSpace:C,FreeDriveSpace:D,FreeDriveSpace:E,FreeDriveSpace:F,FreeDriveSpace:G,FreeDriveSpace:H,FreeDriveSpace:I,FreeDriveSpace:J,FreeDriveSpace:K,FreeDriveSpace:L,FreeDriveSpace:M,FreeDriveSpace:N,FreeDriveSpace:O,FreeDriveSpace:P,FreeDriveSpace:Q,FreeDriveSpace:R,FreeDriveSpace:S,FreeDriveSpace:T,FreeDriveSpace:U,FreeDriveSpace:V,FreeDriveSpace:W,FreeDriveSpace:X,FreeDriveSpace:Y,FreeDriveSpace:Z,ErrorMessage,ErrorItem `
    | Export-CSV -Path $OutputCSV -NoTypeInformation
      Write-Host "Inventory completed. Check $OutputCSV" -ForegroundColor Green

I would try creating an array list of Monitors right above

ForEach ($Monitor in $Monitors)
{
and add custom Monitor objects (PS objects, like the $SI) to it, which contain all the monitor related info that you need ($MonSerialNumber)

Finally use this array of Monitors in the $SI. You will need to change this part as well to get to the desired depth.

$SysCollection | Select-Object

Maybe you could try it with just two PCs - one with a single monitor and one with more? It is also helpful not to use error action silently continue.

Remove the casting to [WMI]$SysInfo. Get-WMIObject is returning a Powershell Object (PSObject) and the cast is attempting to convert to System.Management.ManagementObject. Here are some other tips and methods in Powershell that will make your code:

$computers = '.'

#All output to a $results variable
$results = foreach ($computer in $computers) {
    #Test the connection to see if commputer is online
    if (Test-Connection -ComputerName $computer -Quiet) {
        #Add error handling to ensure that you can connect to WMI
        try {
            #splatting    
            $wmiParams = @{
                ClassName    = 'Win32_LogicalDisk'
                Namespace    = 'Root\CIMV2' 
                #ComputerName = $computer
                Filter       = 'DriveType=3'
                ErrorAction  = 'Stop'
            }
            
            #Get-WMIObject is deprecated, you should use Get-CimInstance
            Get-CimInstance @wmiParams | 
            Select DeviceID, 
                   @{Name='DriveType';Expression={'Fixed Disk'}}, 
                   VolumeName, 
                   @{Name='SizeInGb';Expression={[math]::Round($_.Size / 1gb)}} #Calculated Expression, there is no need to do a for loop to do calculations
        }
        catch {
            Throw ('Error getting WMI information for {0}. {1}' -f $wmiParams.ClassName, $_)
        }
    }
    else {
        'No connection to {0}' -f $computer
    }

}

The switch statements can be done this way as well:

# Assign variable rather than set it in every block
# Use Regex and an pipe to indicate OR                       
$RTStatus = Switch -Regex ( $AV.ProductState ){
    "262144|262160|393216|393232|393488|393472|266240|266256" {"Disabled"} 
    "266240|266256|397312|397328|397584|397568" {"Enabled"} 
    Default {"Unknown"}
}

Thanks,

Emil, sorry I know a little bit about posh but not enough to write that myself. How would I add that to my script. Also is it possible to get the list pending updates working?

Thanks,

Paul

Hi Rob,

How do I do this part that you suggested?

“Remove the casting to [WMI]$SysInfo. Get-WMIObject is returning a Powershell Object (PSObject) and the cast is attempting to convert to System.Management.ManagementObject.”

Regards,

Paul

[WMI]$SysInfo     = Get-WmiObject Win32_ComputerSystem -Namespace "Root\CIMV2" -ComputerName $Computer -ErrorAction SilentlyContinue

casting removed…

$SysInfo     = Get-WmiObject Win32_ComputerSystem -Namespace "Root\CIMV2" -ComputerName $Computer -ErrorAction SilentlyContinue

It sounds like you are just learning. Recommend you minimize the script to perform one WMI call. Test it one 10 computers and add a bad computer name to make sure error handling is working as expected.

Thanks Rob,

Yes I am fairly new to powershell. I basically just want this script to work so that I don’t have to run around the whole building getting all the info from the PC’s.

Hi PaulP123,

i like your script, but even after i comment monitor and update section, its still give me error

Maybe after all updates you can share script here

Thanks.

Hi Brad,

Yes If someone can get it working properly then I’ll gladly share it with you.

I’m at the end of my knowledge with powershell and can’t seem to get any further with it.

Rob has very kindly managed to fix one of the errors I was getting but now I’m getting different ones and don’t know how to resolve them.

 

Well, maybe create custom object array of monitors:

$monitorobjects = New-Object System.Collections.ArrayList
    ForEach ($Monitor in $Monitors)
    {
        If ([System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName) -NE $Null) {
            $MonModel = ([System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName -NE 0)).Replace("$([char]0x0000)","") -join ""
        } 
        Else {
            $MonModel = $Null
        }
        $MonSerialNumber = ([System.Text.Encoding]::ASCII.GetString($Monitor.SerialNumberID -NE 0)).Replace("$([char]0x0000)","") -join ""
        $MonProductNumber = ([System.Text.Encoding]::ASCII.GetString($Monitor.ProductCodeID -NE 0)).Replace("$([char]0x0000)","") -join ""
        $MonManufacturer = ([System.Text.Encoding]::ASCII.GetString($Monitor.ManufacturerName -NE 0)).Replace("$([char]0x0000)","") -join ""


        $props = @{"MonitorSerialNumber"=$MonSerialNumber;
                    "MonitorProductNumber"=$MonProductNumber;
                    "MonitorManufacturer"=$MonManufacturer;
                    "MonitorModel"=$MonModel}
        $monitorobject =New-Object -TypeName psobject -Property $props
        $null=$monitorobjects.Add($monitorobject)
    }

Then check for the number of monitors (1 or 2) before creating $SI. If there are two, create two properties in the $SI, one for the first and one for the second:

Monitor1SerialNo = [String]$monitorobjects[0].MonitorSerialNumber
Monitor2SerialNo = [String]$monitorobjects[1].MonitorSerialNumber

 

Hi All,

I think I have the script working but I’m having a small (hopefully) issue with the monitor part of the script below.

        $MonModelNumber   = ([System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName -Ne 0)).Replace("$([Char]0x0000)","")
        $MonSerialNumber  = ([System.Text.Encoding]::ASCII.GetString($Monitor.SerialNumberID -Ne 0)).Replace("$([Char]0x0000)","")
        $MonProductNumber = ([System.Text.Encoding]::ASCII.GetString($Monitor.ProductCodeID -Ne 0)).Replace("$([Char]0x0000)","")
        $MonManufacturer  = ([System.Text.Encoding]::ASCII.GetString($Monitor.ManufacturerName -Ne 0)).Replace("$([Char]0x0000)","")

If you run it on a PC with dual or more monitors it joins all the data together i.e. if the serial numbers for 2 PC’s are 123456789 and 987654321 it will joint them together when the data is exported to the csv like this 123456789987654321

I would like it to separate the data with a comma like this 123456789, 987654321

I have tried adding -join ", " etc. but I can’t get it to separate the data.

My question is, what and where do I need to add to the code above to make it separate the data?

Thank,

Paul

 

 

 


            

Hi Emil,

I tried adding this but I get the below error.

A positional parameter cannot be found that accepts argument ‘=’.

Many thanks,

Paul