Need some help with this scripts

DHoss 61 Reputation points
2021-05-03T19:46:00.027+00:00

I have compiled this scripts with help of Bhupinder and Thom McKiernan script from internet and all working fine except I do not get the 2nd monitor info if there is 2nd monitor. I would really appreciate if someone can help me with this. I am not good at the pow3rshell but learning.... Thanks in Advance.

Blockquote

$ArrComputers = read-host -Prompt "Enter Computer Name"

function Decode {
If ($args[0] -is [System.Array]) {
}
Else {
"Not Found"
}
}

Clear-Host
foreach ($Computer in $ArrComputers)
{

$wmi_os = Get-WmiObject -class Win32_OperatingSystem -ComputerName $Computer | select CSName,Caption,Version,OSArchitecture,LastBootUptime
switch($wmi_os.Version){
'10.0.10240'{$wmi_build="1507"}
'10.0.10586'{$wmi_build="1511"}
'10.0.14393'{$wmi_build="1607"}
'10.0.15063'{$wmi_build="1703"}
'10.0.16299'{$wmi_build="1709"}
'10.0.17134'{$wmi_build="1803"}
'10.0.17686'{$wmi_build="1809"}
'10.0.18362'{$wmi_build="1903"}
'10.0.18363'{$wmi_build="1909"}
'10.0.19042'{$wmi_build="2009"}
}
$computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
$computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
$computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
$DNName = Get-ADComputer -Filter "Name -like '$Computer'" | select -ExpandProperty DistinguishedName

$computerCPU = get-wmiobject Win32_Processor -Computer $Computer
$computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
$Monitors = Get-WmiObject WmiMonitorID -Namespace root\wmi -ComputerName $Computer

$Monitors = Get-WmiObject WmiMonitorID -RELPATH root\wmi -ComputerName $Computer

 ForEach ( $Monitor in $Monitors) {

$Manufacturer = Decode $Monitor.ManufacturerName -notmatch 0
$Name = Decode $Monitor.UserFriendlyName -notmatch 0
$Serial = Decode $Monitor.SerialNumberID -notmatch 0
$YofM = $Monitor.YearOfManufacture
}

 write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
 "-------------------------------------------------------"
 "Manufacturer    : " + $computerSystem.Manufacturer
 "Model           : " + $computerSystem.Model
 "Serial Number   : " + $computerBIOS.SerialNumber
 "CPU             : " + $computerCPU.Name
 "HDD Capacity    : "  + "{0:N0}" -f ($computerHDD.Size/1GB) + "GB"
 "HDD Space       : " + "{0:P0}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N0}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
 "RAM             : " + "{0:N0}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
 "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
 "Build Version   : " + $wmi_build
 "User logged In  : " + $computerSystem.UserName
 "OU Location     : " + $DNName
 "Last Reboot     : " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
 "Monitor Mfgr    : " + $($Manufacturer)
 "Monitor Name    : " + $($Name)
 "Monior S/N      : " + $($Serial)
 "Monitor Year    : " + $($YofM)

 ""
 "-------------------------------------------------------"

}

Blockquote

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-05-05T05:53:14.15+00:00

    Hi,

    The issue should have been fixed in the last script. Please check if the below one works. I just replaced the Get-CimInstance with Get-WmiObjcet.

    $ArrComputers = read-host -Prompt "Enter Computer Name"  
          
    function Decode {  
        If ($args[0] -is [System.Array]) {  
            [System.Text.Encoding]::ASCII.GetString($args[0])  
        }  
        Else {  
            "Not Found"  
        }  
    }  
          
    Clear-Host  
    foreach ($Computer in $ArrComputers)  
    {    
        $wmi_os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer | select CSName,Caption,Version,OSArchitecture,LastBootUptime  
        switch($wmi_os.Version){  
            '10.0.10240'{$wmi_build="1507"}  
            '10.0.10586'{$wmi_build="1511"}  
            '10.0.14393'{$wmi_build="1607"}  
            '10.0.15063'{$wmi_build="1703"}  
            '10.0.16299'{$wmi_build="1709"}  
            '10.0.17134'{$wmi_build="1803"}  
            '10.0.17686'{$wmi_build="1809"}  
            '10.0.18362'{$wmi_build="1903"}  
            '10.0.18363'{$wmi_build="1909"}  
            '10.0.19042'{$wmi_build="2009"}  
        }  
        $computerSystem = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computer  
        $computerBIOS = Get-WmiObject -Class Win32_BIOS -ComputerName $Computer  
        $computerOS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer    
        $DNName = Get-ADComputer -Filter "Name -like '$Computer'" | select -ExpandProperty DistinguishedName  
        $computerCPU = Get-WmiObject -Class Win32_Processor -ComputerName $Computer  
        $computerHDD = Get-WmiObject -Class Win32_LogicalDisk -Filter drivetype=3 -ComputerName $Computer  
        $Monitors = Get-WmiObject -Class WmiMonitorID -Namespace root\wmi -ComputerName $Computer  
        $Manufacturer = @()  
        $Name = @()  
        $Serial = @()  
        $YofM = @()  
        foreach ( $Monitor in $Monitors) {  
            $Manufacturer += Decode $Monitor.ManufacturerName -notmatch 0  
            $Name += Decode $Monitor.UserFriendlyName -notmatch 0  
            $Serial += Decode $Monitor.SerialNumberID -notmatch 0  
            $YofM += $Monitor.YearOfManufacture  
        }  
                  
        write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan  
            "-------------------------------------------------------"  
            "Manufacturer    : " + $computerSystem.Manufacturer  
            "Model           : " + $computerSystem.Model  
            "Serial Number   : " + $computerBIOS.SerialNumber  
            "CPU             : " + $computerCPU.Name  
            "HDD Capacity    : "  + "{0:N0}" -f ($computerHDD.Size/1GB) + "GB"  
            "HDD Space       : " + "{0:P0}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N0}" -f ($computerHDD.FreeSpace/1GB) + "GB)"  
            "RAM             : " + "{0:N0}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"  
            "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion  
            "Build Version   : " + $wmi_build  
            "User logged In  : " + $computerSystem.UserName  
            "OU Location     : " + $DNName  
            "Last Reboot     : " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)  
            "Monitor Mfgr    : " + $($Manufacturer)  
            "Monitor Name    : " + $($Name)  
            "Monior S/N      : " + $($Serial)  
            "Monitor Year    : " + $($YofM)  
                  
            ""  
            "-------------------------------------------------------"  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

6 additional answers

Sort by: Most helpful
  1. DHoss 61 Reputation points
    2021-05-05T03:54:46.757+00:00

    Hi Ian
    No it did not work as the winRm service is not enable in our environment so CimSession does not work on remote machine. As I said your previous modification works except the year of mfg. Also one more thing I just notice that if I ran the scripts 2nd time for a different computer, it also out the previous computer monitor info with the current one. Your answer would much be appreciated.

    0 comments No comments

  2. DHoss 61 Reputation points
    2021-05-05T08:18:22.497+00:00

    Hi Ian
    Thank you so much for all the help. Yes it is working and again I really appreciate all your effort.

    0 comments No comments

  3. Anonymous
    2021-05-04T05:03:47.25+00:00

    Hi,

    The variable $Manufacturer, $Name, $Serial and $YofM are overwritten. You can use the operator "+=" instead of "=".

    $ArrComputers = read-host -Prompt "Enter Computer Name"  
      
    function Decode {  
    If ($args[0] -is [System.Array]) {  
    [System.Text.Encoding]::ASCII.GetString($args[0])  
    }  
    Else {  
    "Not Found"  
    }  
    }  
      
    Clear-Host  
    foreach ($Computer in $ArrComputers)  
    {  
      
    $wmi_os = Get-WmiObject -class Win32_OperatingSystem -ComputerName $Computer | select CSName,Caption,Version,OSArchitecture,LastBootUptime  
    switch($wmi_os.Version){  
    '10.0.10240'{$wmi_build="1507"}  
    '10.0.10586'{$wmi_build="1511"}  
    '10.0.14393'{$wmi_build="1607"}  
    '10.0.15063'{$wmi_build="1703"}  
    '10.0.16299'{$wmi_build="1709"}  
    '10.0.17134'{$wmi_build="1803"}  
    '10.0.17686'{$wmi_build="1809"}  
    '10.0.18362'{$wmi_build="1903"}  
    '10.0.18363'{$wmi_build="1909"}  
    '10.0.19042'{$wmi_build="2009"}  
    }  
    $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer  
    $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer  
    $computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer  
    $DNName = Get-ADComputer -Filter "Name -like '$Computer'" | select -ExpandProperty DistinguishedName  
      
     $computerCPU = get-wmiobject Win32_Processor -Computer $Computer  
     $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3  
     $Monitors = Get-WmiObject WmiMonitorID -Namespace  root\wmi -ComputerName $Computer  
     #$Monitors = Get-WmiObject WmiMonitorID -RELPATH  root\wmi -ComputerName $Computer  
         ForEach ( $Monitor in $Monitors) {  
      $Manufacturer += Decode $Monitor.ManufacturerName -notmatch 0  
      $Name += Decode $Monitor.UserFriendlyName -notmatch 0  
      $Serial += Decode $Monitor.SerialNumberID -notmatch 0  
      $YofM += $Monitor.YearOfManufacture  
      }  
              
         write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan  
         "-------------------------------------------------------"  
         "Manufacturer    : " + $computerSystem.Manufacturer  
         "Model           : " + $computerSystem.Model  
         "Serial Number   : " + $computerBIOS.SerialNumber  
         "CPU             : " + $computerCPU.Name  
         "HDD Capacity    : "  + "{0:N0}" -f ($computerHDD.Size/1GB) + "GB"  
         "HDD Space       : " + "{0:P0}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N0}" -f ($computerHDD.FreeSpace/1GB) + "GB)"  
         "RAM             : " + "{0:N0}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"  
         "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion  
         "Build Version   : " + $wmi_build  
         "User logged In  : " + $computerSystem.UserName  
         "OU Location     : " + $DNName  
         "Last Reboot     : " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)  
         "Monitor Mfgr    : " + $($Manufacturer)  
         "Monitor Name    : " + $($Name)  
         "Monior S/N      : " + $($Serial)  
         "Monitor Year    : " + $($YofM)  
              
         ""  
         "-------------------------------------------------------"  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  4. Anonymous
    2021-05-05T02:09:39.533+00:00

    Hi,

    Please see if this works better. This will reuse one existing session, not create a new session in every WMI query.

    $ArrComputers = read-host -Prompt "Enter Computer Name"  
          
    function Decode {  
        If ($args[0] -is [System.Array]) {  
            [System.Text.Encoding]::ASCII.GetString($args[0])  
        }  
        Else {  
            "Not Found"  
        }  
    }  
          
    Clear-Host  
    foreach ($Computer in $ArrComputers)  
    {    
        $session = New-CimSession –ComputerName $Computer  
        $wmi_os = Get-CimInstance -ClassName Win32_OperatingSystem -CimSession $session | select CSName,Caption,Version,OSArchitecture,LastBootUptime  
        switch($wmi_os.Version){  
            '10.0.10240'{$wmi_build="1507"}  
            '10.0.10586'{$wmi_build="1511"}  
            '10.0.14393'{$wmi_build="1607"}  
            '10.0.15063'{$wmi_build="1703"}  
            '10.0.16299'{$wmi_build="1709"}  
            '10.0.17134'{$wmi_build="1803"}  
            '10.0.17686'{$wmi_build="1809"}  
            '10.0.18362'{$wmi_build="1903"}  
            '10.0.18363'{$wmi_build="1909"}  
            '10.0.19042'{$wmi_build="2009"}  
        }  
        $computerSystem = Get-CimInstance -ClassName Win32_ComputerSystem -CimSession $session  
        $computerBIOS = Get-CimInstance -ClassName Win32_BIOS -CimSession $session  
        $computerOS = Get-CimInstance -ClassName Win32_OperatingSystem -CimSession $session    
        $DNName = Get-ADComputer -Filter "Name -like '$Computer'" | select -ExpandProperty DistinguishedName  
        $computerCPU = Get-CimInstance -ClassName Win32_Processor -CimSession $session  
        $computerHDD = Get-CimInstance -ClassName Win32_LogicalDisk -Filter drivetype=3 -CimSession $session  
        $Monitors = Get-CimInstance -ClassName WmiMonitorID -Namespace root\wmi -CimSession $session  
        $Manufacturer=@()  
        $Name=@()  
        $Serial=@()  
        $YofM=@()  
        foreach ( $Monitor in $Monitors) {  
            $Manufacturer += Decode $Monitor.ManufacturerName -notmatch 0  
            $Name += Decode $Monitor.UserFriendlyName -notmatch 0  
            $Serial += Decode $Monitor.SerialNumberID -notmatch 0  
            $YofM += $Monitor.YearOfManufacture  
        }  
                  
        write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan  
            "-------------------------------------------------------"  
            "Manufacturer    : " + $computerSystem.Manufacturer  
            "Model           : " + $computerSystem.Model  
            "Serial Number   : " + $computerBIOS.SerialNumber  
            "CPU             : " + $computerCPU.Name  
            "HDD Capacity    : "  + "{0:N0}" -f ($computerHDD.Size/1GB) + "GB"  
            "HDD Space       : " + "{0:P0}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N0}" -f ($computerHDD.FreeSpace/1GB) + "GB)"  
            "RAM             : " + "{0:N0}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"  
            "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion  
            "Build Version   : " + $wmi_build  
            "User logged In  : " + $computerSystem.UserName  
            "OU Location     : " + $DNName  
            "Last Reboot     : " + $computerOS.LastBootUpTime  
            "Monitor Mfgr    : " + $($Manufacturer)  
            "Monitor Name    : " + $($Name)  
            "Monior S/N      : " + $($Serial)  
            "Monitor Year    : " + $($YofM)  
                  
            ""  
            "-------------------------------------------------------"  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.