Public/Get-specMonitorInfo.ps1
Function Get-specMonitorInfo() { <# .SYNOPSIS Retrieves information about connected monitors including their manufacturer and model. .DESCRIPTION The function gathers information on the connected monitors by querying the WMI (Windows Management Instrumentation) namespace for monitor details. The function retrieves the monitor's manufacturer and model name, and returns the information as a custom PowerShell object. .PARAMETER AsJson If specified, the monitor information will be returned in a single line JSON format without null characters. .OUTPUTS Custom PowerShell object containing: - Manufacturer: The name of the monitor's manufacturer. - Model: The model name of the monitor. If the -AsJson switch is used, the output will be a JSON string instead. .EXAMPLE PS> Get-specMonitorInfo This example retrieves the manufacturer and model of connected monitors and displays the information as custom objects. .EXAMPLE PS> Get-specMonitorInfo -AsJson This example retrieves the manufacturer and model of connected monitors and returns the information in a single line JSON format. .NOTES Author: .leon.roze Version 1.0 - initial release -(Refactored slightly by owen.heaume - Add comment-based help, change switch name to something more informative, add error checking so try \ catch can be used) #> [CmdletBinding()] param ( [switch]$AsJson ) $Monitors = @() try { $MonitorWmi = Get-CimInstance WmiMonitorID -Namespace root/WMI -ErrorAction Stop -ErrorVariable ev $MonitorWmi | % { $ManName = '' $ModelName = '' $_.ManufacturerName | % { $ManName += [char]$_ } $_.UserFriendlyName | % { $ModelName += [char]$_ } $Monitors += [PSCustomObject]@{ Manufacturer = $ManName Model = $ModelName } } if ($PSBoundParameters.ContainsKey('AsJson')) { Return ($Monitors | ConvertTo-Json).Replace('\u0000', '') } Else { Return $Monitors } } catch { Write-Error "Failed to get Monitors: $_" } } |