Customising the Powershell two-state monitor

Hi, I am currently looking to check if a server is responding to SMTP on Port 25. I’m not too great at scripting so can’t really make sense of what the management pack in SCOM is doing.

$smtpServer = "mail.example.com"
$smtpPort = 25

try {
    $tcpClient = New-Object System.Net.Sockets.TcpClient
    $tcpClient.Connect($smtpServer, $smtpPort)
    $tcpClient.Close()
    Write-Host "SMTP is reachable on port $smtpPort for $smtpServer."
} catch {
    Write-Host "SMTP is not reachable on port $smtpPort for $smtpServer."
}

Do you know how to get this to work with the management pack?

Any help appreciated?

Hi Matt, and welcome to the community.

I’m running under the assumption that this is with the PowerShell Community MP, as it sounds a bit like that (if I’m incorrect in that assumption please do correct me!).

If so there’s a good run through video here.

But the gist of things is that you’ll be using the SCOM API to generate a ‘Property Bag’ which you’ll insert your results into. That property bag gets sent back to SCOM and then each monitor gets the properties/values that it cares about out of the bag.

Rather than using write-host you’ll be calling the addValue method to add something to the bag. For a simple two-state monitor you may want to simply do something like addValue('state', 'error') or addValue('state', 'healthy').

If you want to use different SMTP servers/ports you will need to pass in arguments. Arguments are passed in as a simple string. If you need to provide multiple arguments you’ll need to delimit them with something like a comma, semi-colon, etc. and then split them out into variables from there.

As you don’t have a console to print to, debugging is a bit trickier. For simple scenarios I usually have a debug argument, and then use events to troubleshoot using the APIs logScriptEvent method. For more complex scripts I tend to use transcript logging. However in this case I suspect logging would be overkill!

Hopefully that’s a good whistle-stop tour and will get you going. Let us know if you have any further questions!

Cheers,
EP

This should do what you are looking for.

$StopWatch = [system.diagnostics.stopwatch]::startNew()
$EventID = 911
$ScomAPI = New-Object -comObject “MOM.ScriptAPI”
$PropertyBag = $ScomAPI.CreatePropertyBag()
$Script = “ScriptName.ps1”

$API.LogScriptEvent($Script, $EventID, 0, “Starting $Script”)

Try {
$smtpServer = “mail.example.com
$smtpPort = 25

$tcpClient = New-Object System.Net.Sockets.TcpClient
$tcpClient.Connect($smtpServer, $smtpPort)
$tcpClient.Close()

$API.LogScriptEvent($Script, $EventID, 0, “YOUR TEXT HEREnnThe script ran for $($StopWatch.Elapsed.TotalSeconds) seconds”)
$PropertyBag.AddValue(“State”, 0)
$PropertyBag.AddValue(“MessageText”, “YOUR TEXT HERE”)
$PropertyBag
} catch {
$API.LogScriptEvent($Script, $EventID, 1, “YOUR TEXT HEREnnThe script ran for $($StopWatch.Elapsed.TotalSeconds) seconds”)
$PropertyBag.AddValue(“State”, 1)
$PropertyBag.AddValue(“MessageText”, “YOUR TEXT HERE: $($_.Exception.Message)”)
$PropertyBag
}

I’m a little late to the party here…

Why do you want to use PowerShell instead of the native TCP port monitoring modules? MS provides decent port monitoring out of the box. Using the ‘TCP Port’ Management Pack template.

If you want to write your own monitor type take a look at Microsoft.SystemCenter.SyntheticTransactions.TCPPortCheckProbe in the Synthetic Transactions Library MP. Microsoft.SystemCenter.SyntheticTransactions.TCPPortCheckProbe (ProbeActionModuleType)

Shameless plug for my SMF MPs…
The SMF MPs have TCP port monitoring using Windows and Linux as watchers nodes. KeithRochester/Standard-Monitoring-Framework: Standard Monitoring Framework (github.com)