SquaredUp Powershell Community MP Two State Monitor Help Required

Hello,

I wonder if anyone can please assist me, I am trying to create a 2 State Monitor to alert if a FTP connection fails. I am using the PowerShell script was written by Peter Zerger (see http://www.systemcentercentral.com/monitoring-ftp-site-availability-in-opsmgr-using-powershell/)

The script works perfectly without any issues when running this from PowerShell, But the 2 State monitor never changes, it always remains in a healthly state, not sure what to try next also is there away of checking what values are being sent back in the Property bag?. I am new to PowerShell so can anyone assist please.

 

$ScomAPI = New-Object -comObject “MOM.ScriptAPI”

$PropertyBag = $ScomAPI.CreatePropertyBag()

 

Get the object used to communicate with the server.

$Request = [System.Net.WebRequest]::Create(“ftp:ftp URL goes in here/”)

$Request.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails

This example assumes the FTP site uses anonymous logon.

Username/password not real

$Request.Credentials = New-Object System.Net.NetworkCredential “username”,password

$Response = $Request.GetResponse()

$ResponseStream = $Response.GetResponseStream()

Read and display the text in the file

$Reader = new-object System.Io.StreamReader $Responsestream

[System.Console]::Writeline($Reader.ReadToEnd())

Display Status

“Download Complete, status:”

$response.StatusDescription

 

if ($response.StatusDescription -match ‘226’){

#{“Response code is 226. Health is GOOD”} #testing only. Comment out this line for production use.

#Write-Host “We hit a TRUE match”

$PropertyBag.AddValue(“state”,”GOOD”)

#Submit Property Bag

#$API.Return($Bag) #for testing only. Comment out this line for production use.

$PropertyBag #For production use. Un-comment this line for production use.

}

else {

#{“Response code not 226. Health is BAD”} #testing only. Comment out this line for production use.

#If not exists STATE=BAD

#Write-Host “We hit a False match”

$PropertyBag.AddValue(“state”,”BAD”)

#Submit Property Bag

#$API.Return($Bag) #for testing only. Comment out this line for production use.

$PropertyBag #For production use. Un-comment this line for production use.

}

Close Reader and Response objects

$Reader.Close()

$Response.Close()

 

The Monitor Unhealthily Expression reads:

Property[@Name=’state’] Equals GOOD (Please note I have set this intentionally to generate an alert)

so in theory it should generate an alert

 

The Monitor Healthy Expression reads:

Property[@Name=’state’] Equals BAD

3 Likes

Strange things are happening, came in this Morning and the Monitor is now working, should know better with SCOM, also wait a Day and test. :slight_smile:

I’ve had oddities like things sometimes taking an hour or two to start gathering metrics when I use powershell. It caught me out at first.

Also I have not found a way to properly display the contents of a propertybag during script creation and debugging. Found a few articles, but nothing worked.

Remember that your script must submit a property bag unless your health expressions also test for the existence of a value - if the script hits an exception and terminates before it returns anything, your health/unhealthy expressions won’t match. Generally it’s easier to use Try/Catch/Finally behaviour in PowerShell to ensure something is returned, rather then start adding Exists checks into your health expressions.

The property bag that triggered an health state change is displayed in the alert context (view the monitor in Health explorer, in the right window select the state change and look in the lower half).

However, since the monitors in the MP do not support On-Demand detection (the feature that makes the recalculate button function, but can have performance impacts with maintenance mode), it won’t show anything when the monitor first starts up into a healthy state.

If you want to see property bag output when authoring a script, you can do this by calling the .Return method on your Mom.ScriptAPI object and passing in the property bag, but be aware this will only write the XML contents to host - you can’t capture the content (save in a variable, write to file) so it’s only useful if you are running the script interactively.

$api = new-object -comObject "MOM.ScriptAPI"
$p = $api.CreatePropertyBag()
$p.AddValue("foo","bar")
$api.Return($p)

Hope that helps!

1 Like

Hi Darren,

I only discovered today that to view the contents of the PropertyBag you need to run the SCRIPT in PowerShell and NOT PowerShell ISE

When you run the script in PowerShell ISE you will only see the following results:
System.__ComObject

Running the Same script for PowerShell will display the values in the PropertyBag in XML Format

for example using Vyper example below:

PS D:\scripts> .\foobar.ps1
barPS D:\scripts>

hope this helps next time you need to view the contents of the PropertyBag

Hi Vyper,

Thank you for your help, great example. it made a couple of things clear for me

Thanks Prakash! I will try that next time. :slight_smile: