Created
January 18, 2018 04:29
-
-
Save camusicjunkie/24354ddad6db10b29a45d95649f7bf01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-MonitorInfo { | |
<# | |
.SYNOPSIS | |
Gets computer and monitor information | |
.DESCRIPTION | |
Gets computer information and monitor information for all | |
connected monitors. Each monitor is numbered for as many | |
monitors as you have connected | |
.PARAMETER ComputerName | |
Specifies a remote computer. The default is the local computer | |
.EXAMPLE | |
Get-MonitorInfo -ComputerName localhost | |
.EXAMPLE | |
'localhost' | Get-MonitorInfo | |
.INPUTS | |
[System.String] | |
.OUTPUTS | |
[Custom.MonitorInfo] | |
#> | |
[CmdletBinding()] | |
Param ( | |
[Parameter(ValueFromPipeline, | |
ValueFromPipelineByPropertyName)] | |
[ValidateNotNullOrEmpty()] | |
[Alias('computer', 'cn', 'workstation')] | |
[string[]] $ComputerName = $env:COMPUTERNAME | |
) | |
begin { | |
Write-Verbose "[BEGIN ] $(Get-Date) Starting: $($MyInvocation.MyCommand)" | |
$PSDefaultParameterValues = @{ | |
'Get-CimInstance:Verbose' = $false | |
} | |
} | |
process { | |
foreach ($computer in $ComputerName) { | |
Write-Verbose "[PROCESS] $(Get-Date) Processing $computer" | |
try { | |
$compParams = @{'ClassName' = 'win32_ComputerSystem'} | |
$biosParams = @{'ClassName' = 'win32_Bios'} | |
$monParams = @{'ClassName' = 'wmiMonitorID' | |
'Namespace' = 'root\wmi'} | |
if ($PSBoundParameters.ContainsKey('ComputerName')) { | |
Write-Verbose "Trying to create session to $computer" | |
$params = @{'ComputerName' = $computer | |
'ErrorAction' = 'Stop' | |
'Verbose' = $false | |
} | |
$session = New-CimSession @params | |
Write-Verbose "Session was created to $computer" | |
# Add cimSession parameter to all param hashtables | |
$compParams.Add('CimSession', $session) | |
$biosParams.Add('CimSession', $session) | |
$monParams.Add('CimSession', $session) | |
} | |
Write-Verbose "Collecting info from $computer" | |
$comp = Get-CimInstance @compParams | |
$bios = Get-CimInstance @biosParams | |
$monitor = Get-CimInstance @monParams | |
Write-Verbose "Creating custom PSObject from collected info" | |
$obj = [pscustomobject] @{ | |
'ComputerName' = $computer | |
'ComputerModel' = $comp.Model | |
'ComputerSerial' = $bios.SerialNumber | |
} | |
# Looping through all connected monitors | |
$i = 1 | |
foreach ($mon in $monitor) { | |
Write-Verbose "Collecting info from Monitor$i" | |
$monProps = [ordered] @{ | |
"MonitorModel$i" = | |
[System.Text.Encoding]::Default.GetString($mon.UserFriendlyName) | |
"MonitorSerial$i" = | |
[System.Text.Encoding]::Default.GetString($mon.SerialNumberID) | |
} | |
# Adding monitor info to custom psobject | |
$obj | Add-Member -NotePropertyMembers $monProps | |
$i++ | |
} | |
$obj.PSObject.TypeNames.Insert(0,'Custom.MonitorInfo') | |
Write-Output $obj | |
} | |
catch { | |
Write-Warning "Could not create a session to $computer" | |
} | |
finally { | |
if ($session) { | |
Write-Verbose "Removing session from $computer" | |
$session | Remove-CimSession | |
} | |
} | |
} | |
} | |
end { | |
Write-Verbose "[END ] $(Get-Date) Ending: $($MyInvocation.MyCommand)" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment