Advertisement
ardrgz

Inventory Monitors with PowerShell

Apr 11th, 2015
1,759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-Monitor{
  2.     [cmdletbinding()]
  3.  
  4.     Param(
  5.         [Parameter(Mandatory=$True,Position=0)]
  6.         [string[]]$ComputerName
  7.     )
  8.  
  9.     Begin{}
  10.  
  11.     Process{
  12.         Foreach($Computer in $ComputerName){
  13.             If(Test-Connection -ComputerName $Computer -Count 1 -Quiet){
  14.                 Try{
  15.                     $Monitors = get-wmiobject -ComputerName $Computer -Class wmimonitorid -Namespace root/wmi -ErrorAction Stop
  16.                     Foreach($Monitor in $Monitors){
  17.                         If($Monitor.UserFriendlyName -ne $null){$Model = [System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName)}
  18.                         Else{$Model = "N/A"}
  19.                         If($Monitor.SerialNumberID -ne $null){
  20.                             $SerialNumber = [System.Text.Encoding]::ASCII.GetString($Monitor.SerialNumberID)
  21.                             If($SerialNumber -eq 0){
  22.                                 $SerialNumber = "N/A"
  23.                             }
  24.                         }
  25.                         Else{$SerialNumber = "N/A"}
  26.                         New-Object -TypeName PSCustomObject -Property @{
  27.                             ComputerName = $Computer
  28.                             Model = $Model
  29.                             SerialNumber = $SerialNumber
  30.                         }
  31.                     }
  32.                 }
  33.                 Catch{
  34.                     Write-Warning -Message "Cannot query WMIMonitorID WMI class on $Computer. $($_.Exception.Message)"
  35.                 }
  36.             }
  37.             Else{
  38.                 Write-Warning -Message "Connection to remote server $Computer failed."
  39.             }
  40.         }
  41.     }
  42.  
  43.     End{}
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement