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?
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!).
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!