Monitor screen resolution with SCOM and PowerShell

In a previous post I wrote about how you can extend SCOM basic functionality with PowerShell scripts. In this post I will show how you can use SCOM and PowerShell to monitor screen resolution on a certain computer.

Image you have a kiosk station where you must show the application in a specific screen resolution. I someone changes the screen resolution, the application you’ve worked so hard on (or more likely your developer) is mis-displayed on the screen. Enter’s SCOM with its PowerShell capabilities…

Now, there are different ways of approaching this. I chose the following way – an agent installed on the target machine (kiosk machine) running a PowerShell script the queries the local WMI.

Other ways you can achieve the exact same way:

  1. a remote computer querying the kiosk machine WMI
  2. a VBscript vs PowerShell

Since I wanted this script to be generic, fit any environment, I wrote the script that receives as a parameter the desired resolution.


# NAME: Test.Screen.Resolution.Ps1
# Arguments passed to Script:
# $DesiredWidth = Desired width of the screen resolution
# $DesiredHeight = Desired hight of the screen resolution
#
# CREATED       : 22/10/2015
# CREATED BY    : Yury Kissin, XperInt
# Contact       : Yury@XperInt.com
#==========================================================================

Param($Arguments)

$ErrorActionPreference = "stop"
$Message = $null

$PropertyBagAPI = New-Object -ComObject "MOM.ScriptAPI"
$PropertyBag = $PropertyBagAPI.CreatePropertyBag()

$PropertyBagAPI.LogScriptEvent("Test.Screen.Resolution.Ps1",9999,0,"Begin executing monitoring script")

$Args = $Arguments.Split(";")
$DesiredWidth = $Args[0]
$DesiredHeight = $args[1]

Try{
$ScreenResolution = Get-WmiObject -Query "SELECT * FROM Win32_DesktopMonitor"
$PropertyBagAPI.LogScriptEvent("Test.Screen.Resolution.Ps1",9999,0,"Resolution is " + $ScreenResolution.ScreenWidth + "x" + $ScreenResolution.ScreenHeight)
if(!(($ScreenResolution.ScreenHeight -eq $DesiredHeight) -and ($ScreenResolution.ScreenWidth -eq $DesiredWidth))){
$Message = "Screen resolution for computer is not optimal. Current config is " + $ScreenResolution.ScreenWidth + "x" + $ScreenResolution.ScreenHeight
}
}
Catch [Exception]{
$Message = "Error running script Test.Screen.Resolution.Ps1. Error was: " + $_.Exception.Message
$PropertyBagAPI.LogScriptEvent("Test.Screen.Resolution.Ps1",9999,0,"Error running script: " + $_.Exception.Message)
}
if($Message){
$PropertyBag.AddValue("State","Critical")
$PropertyBag.AddValue("Message",$Message)
}
else{
$PropertyBag.AddValue("State","Healthy")
}

$PropertyBag

 

Let me explain some of the script lines, starting with:


$PropertyBagAPI.LogScriptEvent("Test.Screen.Resolution.Ps1",9999,0,"Begin executing monitoring script")

Basically this line is done for debugging but you can also use it on a regular basis. What it does is it writes an event in the ‘Operations Manager’ log on the machine its running. that way you can tell that the script has began its work.

The next lines are the logic of the script. I start by querying the WMI:


$ScreenResolution = Get-WmiObject -Query "SELECT * FROM Win32_DesktopMonitor"

And then compare it to the desired result. If its different, than I enter a message that says the resolution is other than the desired.

You can notice the ‘Try/Catch’ statement here. Basically, I hate it when my scripts fail so I try to work with Try/Catch more often. That way, when something goes wrong, I can return the exception and debug it later.

 

Incoming search terms:

  • how to change the screen resolution of SCOM
  • powershell get screenresolution
  • powershell screen resolution

What do you think about this post?