Displaying monitor model and input it is using

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Andy Smith Andy Smith 7 months, 3 weeks ago.

  • Author
    Posts
  • #37592
    Profile photo of Andy Smith
    Andy Smith
    Participant

    Evening all

    After hours and hours of searching on how to display monitor model i found what i needed. I've adapted it for my need and was wondering if it would be possible to add 2 classes together from WmiMonitorBasicDisplayParams & WmiMonitorID.

    Basically i want an output that gives me the Model & Input it's using. WmiMonitorBasicDisplayParams-VideoInputType will give me an output 1 = Digital & 0 = Analog and the rest of the script is below... Please help

    Function MonitorInfo {
    
    $test = "IP or PC name"
            $ActiveMonitors = Get-WmiObject -Namespace "ROOT\WMI" -ComputerName $test -Query "SELECT * FROM WmiMonitorID WHERE Active='True'"
            $monitorInfo = @()
    
            foreach ($monitor in $ActiveMonitors) {
                $mon = @{}
                $name = $null
    
                 foreach($ch in $monitor.UserFriendlyName) {
                    if($ch -ne '00') {
                        $name += [char]$ch
                    }
                }
    
                $mon = $name
    
                $monitorInfo += $mon
            }
            $monitorInfo
        }
    
    $Moninfo = MonitorInfo
    
    # Write out to file(s)
    
    $Moninfo
    
    

    This is the line for the Inout type

    Get-WmiObject -Namespace 'root\wmi' -ComputerName $test -class WmiMonitorBasicDisplayParams | select VideoInputType

    Cheers

  • #37597
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    Try to create a custom object, and combine the results in there. A example:

    [PSCustomObject]@{
        MonitorName = $monitor.UserFriendlyName
        MonitorMode = $monitor.Model
    }
    

    More information here: https://powershell.org/kb/new-object-psobject-vs-pscustomobject/

  • #37601
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    This should do what you want and can serve as a template for adding other properties or combining other classes

    function Get-MonitorInfo {
        [CmdletBinding()]
        param(
            $computername = $env:COMPUTERNAME
        )
    
        $monitors =  Get-CimInstance -Namespace root\wmi -ClassName WmiMonitorId -Filter "Active = '$true'" -ComputerName $computername
        
        foreach ($monitor in $monitors) {
            
            $in = ($monitor.InstanceName).Replace('\', '\\')
            Write-Verbose -Message $in
            $dp = Get-CimInstance -Namespace root\wmi -ClassName WmiMonitorBasicDisplayParams -Filter "InstanceName = '$in'" -ComputerName $computername
            
            $name = ''
    
            foreach ($c in $monitor.UserFriendlyName){
                if ($c -ne '00'){$name += [char]$c}
            }
    
    
            $type = 'Unknown'
            switch ($dp.VideoInputType){
                0 {$type = 'Analog'}
                1 {$type = 'Digital'}
            }
            
            New-Object -TypeName PSObject -Property @{
                Name = $name
                Type = $type
            }
        }
    }
    
  • #37602
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    Richard, why do you use New-Object vs. the type accelerator?

  • #37631
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    Because its a simpler explanation to someone getting started with this. You end up at the same place:

    PS> $x = [PSCustomObject]@{
    >> Name = 'name'
    >> Type = 'type'
    >> }
    PS> $x
    
    Name Type
    ---- ----
    name type
    
    
    PS> $x | gm
    
    
       TypeName: System.Management.Automation.PSCustomObject
    
    Name        MemberType   Definition
    ----        ----------   ----------
    Equals      Method       bool Equals(System.Object obj)
    GetHashCode Method       int GetHashCode()
    GetType     Method       type GetType()
    ToString    Method       string ToString()
    Name        NoteProperty string Name=name
    Type        NoteProperty string Type=type
    
    
    PS> $y = New-Object -TypeName PSObject -Property @{
    >> Name = 'name'
    >> Type = 'type'
    >> }
    PS> $y
    
    Name Type
    ---- ----
    name type
    
    
    PS> $y | gm
    
    
       TypeName: System.Management.Automation.PSCustomObject
    
    Name        MemberType   Definition
    ----        ----------   ----------
    Equals      Method       bool Equals(System.Object obj)
    GetHashCode Method       int GetHashCode()
    GetType     Method       type GetType()
    ToString    Method       string ToString()
    Name        NoteProperty string Name=name
    Type        NoteProperty string Type=type
    

    But its more obvious what is happening. Also New-Object works in more versions of PowerShell than [pscustomobject]@{}

  • #37634
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    Fair enough! Thanks Richard and see you next week at psconf!

  • #37778
    Profile photo of Andy Smith
    Andy Smith
    Participant

    Thanks guys it's all working 🙂

You must be logged in to reply to this topic.