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?
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
... and one remedy is to replace
$Monitors = $MonitorsCMD
by
$Monitors = @($MonitorsCMD)
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.
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.
@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
WIth one monitor $Monitors.Count is null. ;)