Link to home
Start Free TrialLog in
Avatar of SeeDk
SeeDk

asked on

Powershell script to print out monitors installed on PC

I modified a script I found on another forum for this purpose. It works great for any PC with 2+ monitors but for a PC with only one monitor it returns no values.
What should be changed to fix this?

$LogFile = "C:\monitors.txt"
"ComputerName,Manufacturer,Serial" | Out-File $LogFile
echo "ComputerName,Monitor,Serial"


foreach ($computername in (Get-Content -Path C:\comptest.txt))
{$MonitorsCMD = Get-WmiObject WmiMonitorID -Namespace root\wmi -ComputerName $computername

$Monitors = $MonitorsCMD
$LogFile = "C:\monitors.txt"

For($i=0; $i -le ($Monitors.Count - 1); $i++)
{
    $nm = $Monitors[$i].UserFriendlyName -notmatch 0
    $Name = ""
    If ($nm -is [System.Array]) {
    For($j=0; $j -le ($nm.Count - 1); $j++)
    {
        $Name = $Name + [char]$nm[$j]
    }
    }
    $sr = $Monitors[$i].SerialNumberID -notmatch 0
    If ($sr -is [System.Array]) {
    $Serial = ""
        For($j=0; $j -le ($sr.Count - 1); $j++)
    {
        $Serial = $Serial + [char]$sr[$j]
    }
    }
	
	"$ComputerName,$Name,$Serial" | Out-File $LogFile -append
    echo "$ComputerName,$Name,$Serial"
}
}

import-csv $LogFile -delimiter "`t" | export-csv monitors.csv

Open in new window

SOLUTION
Avatar of Jeremy Weisinger
Jeremy Weisinger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jeremy Weisinger
Jeremy Weisinger

This line is why it doesn't work with one monitor:
For($i=0; $i -le ($Monitors.Count - 1); $i++)

Open in new window

WIth one monitor $Monitors.Count is null. ;)
... and one remedy is to replace
$Monitors = $MonitorsCMD

Open in new window

by
$Monitors = @($MonitorsCMD)

Open in new window

making it an array, even if empty, and an array always has a count. Your original code fails because a single object does not have a count (for most object types), and an non-existing property is converted to $null respective 0 or "" depending on the target type it is used with in an expression.
Avatar of SeeDk

ASKER

Thank you both!
@footech's script works with no issue.
@Jeremy's script worked for PC's with one monitor but was reporting only one monitor on all PCs.
Oops! I forgot to take out my select statement when I tested it. :-)

$LogFile = "C:\monitors.txt"
"ComputerName,Manufacturer,Serial" | Out-File $LogFile
echo "ComputerName,Monitor,Serial"

foreach ($computername in (Get-Content -Path C:\comptest.txt)) {
    $Monitors = Get-WmiObject WmiMonitorID -Namespace root\wmi -ComputerName $computername

    $LogFile = "C:\monitors.txt"
    Foreach ($monitor in $Monitors){
        $nm = $Monitor.UserFriendlyName -notmatch 0
        $Name = ""
        If ($nm -is [System.Array]) { 
            For($j=0; $j -le ($nm.Count - 1); $j++){
            $Name = $Name + [char]$nm[$j]
            }
        }
        $sr = $Monitor.SerialNumberID -notmatch 0
        If ($sr -is [System.Array]) {
            $Serial = ""
            For($j=0; $j -le ($sr.Count - 1); $j++){
            $Serial = $Serial + [char]$sr[$j]
            }
        }
	
	    "$ComputerName,$Name,$Serial" | Out-File $LogFile -append
        echo "$ComputerName,$Name,$Serial"
    }
}

import-csv $LogFile -delimiter "`t" | export-csv monitors.csv

Open in new window