New to Powershell and am confused.

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Erik Sundin Erik Sundin 1 week, 3 days ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #54213
    Profile photo of Larry Gross
    Larry Gross
    Participant

    The command below works but with two monitors the second serial number is truncated I do not know why. Now obviously being new I did not write the code below.

    $monitorserial = gwmi WmiMonitorID -computername $x -Namespace root\wmi | ForEach-Object {($_.UserFriendlyName -notmatch 0 | foreach {[char]$_}) -join ""; ($_.SerialNumberID -notmatch 0 | foreach {[char]$_}) -join ""}
        Write-Host ("Monitor Model(s) and Serial Number(s) is $monitorserial") -ForegroundColor Blue
        Write-Host "`n"
    
    • This topic was modified 1 week, 6 days ago by Profile photo of Larry Gross Larry Gross.
    #54214
    Profile photo of Don Jones
    Don Jones
    Keymaster

    If you just run:

    gwmi WmiMonitorID -Namespace root\wmi | fl *

    locally on an HP, is all the information present?

    #54216
    Profile photo of Larry Gross
    Larry Gross
    Participant

    Thank you for the reply. I was doing some further research and found out that if there is only one monitor attached to a system the number comes out correctly. Also I should have been clearer on what I am doing. I was verifying serial numbers on remote systems that I do not have access to but know the system and monitors are on. I verified this on a system with only one hp monitor attached.

    • This reply was modified 1 week, 6 days ago by Profile photo of Larry Gross Larry Gross.
    #54226
    Profile photo of Mike Eyler
    Mike Eyler
    Participant

    Try this:

    $Workstations = @('pc1','pc2')

    foreach ($Workstation in $Workstations)
    {

    $SerialNumbers = (Get-WmiObject -Namespace root\wmi -Class WmiMonitorID -ComputerName $Workstation | Where-Object {(($_.UserFriendlyName -and $_.SerialNumberID) -notmatch 0)}).SerialNumberID -join ""
    $Workstation
    $SerialNumbers
    }

    I did not test on a dual monitor system. You may need to do further text manipulation(ex, -Split) if it returns two or more serial numbers.

    • This reply was modified 1 week, 4 days ago by Profile photo of Mike Eyler Mike Eyler.
    #54244
    Profile photo of Erik Sundin
    Erik Sundin
    Participant

    Just wanted to add that this code is a bit broken, excluding zeros (-notmatch 0) yield some weird results. As an example, on my system the models DELL U2713HM and LA2405 becomes DELL U713HM and LA405 (the code works if I don´t exclude the zeros from .UserFriendlyName).

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.