SourCitrix
SourCitrix

Reputation: 55

PowerShell Monitor SerialNumberID

I'm trying to get the serial numbers of the monitors connected to machines on a network. I am able to get the serials and separate each monitor into its own variable. My problem is when I only have one monitor connected to a machine I only get the first character of the serial.

function GetMonitorSerial () {
    $Monitor = Get-WmiObject -NameSpace root\wmi -Class wmiMonitorID -EA 0 | ForEach-Object {
        $([System.Text.Encoding]::Ascii.GetString($($_.SerialNumberID)))
    }

    $MON1 = $Monitor[0]
    $MON2 = $Monitor[1]
    Write-Host "Monitor #1: " $MON1
    Write-Host "Monitor #1: " $MON2
}

GetMonitorSerial

Upvotes: 0

Views: 2375

Answers (3)

big J88
big J88

Reputation: 21

Here an enhanced function to get Monitor information

function Get-MonitorInfo {

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

    $monitors=[pscustomobject]@()

    foreach ($mon in gcim WmiMonitorID -Namespace root\wmi ) {
        $monitors+=[pscustomobject]@{
            Active             = $mon.active    
            InstanceName       = $mon.InstanceName
            ManufacturerName   = Decode $Mon.ManufacturerName 
            ProductCodeID      = Decode $Mon.ProductCodeID 
            SerialNumberID     = Decode $Mon.SerialNumberID
            Userfriendlyname   = Decode $Mon.UserFriendlyName 
            YearOfManufactur   = $mon.YearOfManufacture
            WeekOfManufacture  = $mon.WeekOfManufacture

         }
    }
    $monitors
}
Get-MonitorInfo

Upvotes: 0

TopG
TopG

Reputation: 1

Here is a beautiful and updated powershell method that displays all monitors names and serial numbers.

function Get-MonitorSerial {
    $monitors = Get-CimInstance -Namespace root/wmi -ClassName WmiMonitorID -ErrorAction SilentlyContinue |
                Where-Object { $_.SerialNumberID -ne $null } |
                ForEach-Object {
                    $name = [System.Text.Encoding]::ASCII.GetString($_.UserFriendlyName)
                    $serial = [System.Text.Encoding]::ASCII.GetString($_.SerialNumberID)
                    if ($name) {
                        Write-Host "${name}: $serial"
                    } else {
                        Write-Host "Monitor: $serial"
                    }
                }
}

Get-MonitorSerial

Upvotes: 0

Modro
Modro

Reputation: 436

The @ indicates an array. @(objects)

function GetMonitorSerial () {
        $Monitor = @(Get-WmiObject -NameSpace root\wmi -Class wmiMonitorID -EA 0 | ForEach-Object {
            $([System.Text.Encoding]::Ascii.GetString($($_.SerialNumberID)))
        })

        $MON1 = $Monitor[0]
        $MON2 = $Monitor[1]
        Write-Host "Monitor #1: " $MON1
        Write-Host "Monitor #2: " $MON2
    }

    GetMonitorSerial

Upvotes: 1

Related Questions