Returning warning status from Powershell script

Hello,

My question is, how can I return a warning status from a Powershell script? Success or critical is easy to achieve but it doesn’t seem like returning a warning.

I have a script that I’m using to query the status of SAP services on our servers. If SAP is down it detects an error and returns that, however, we get false positives or blips that mean the monitor turns red for a few minutes when actually there’s nothing wrong.

What I want to be able to do is return a warning initially to indicate something may be up and change the monitor to yellow, then if it’s just a blip that can turn green again on the next check, if there is still an issue it will go red.

Below is my code to return that I’ve been fiddling with including and excluding Success and I read somewhere that I might be able to return Level with a value of warning… but that doesn’t seem to do anything.

$result = New-Object -TypeName PSobject -Property @{
‘Success’ = $return
‘Description’ = $description
‘Level’ = $level
}
return $result

Any help would be appreciated.
Thanks.

1 Like

Hello,

For anyone who might be interested, in the end I figured out the combination of statuses I needed to return.

May script sets $broken to true if SAP isn’t in a running state on a server.

if ($broken -eq $true) {
$description = ”The $($env) environment has an issue with$($brokenServers) server(s)”
$monitor = Get-SCOMMonitoringObject -displayname ”Server Availability” -computername ”<servername>”
if ($monitor.HealthState -eq ”Success”) {
$return = $false
$level = ’warning’
} else {
$return = $false
$level = ’error’
}
}

$result = New-Object -TypeName PSobject -Property @{
’Success’ = $return
’Description’ = $description
’Level’ = $level}
return $result

So basically I have to set ’Success’ to $false if it’s any state other than successful, and then use level to return either warning or error. The first time it happens it sets to warning then on the subsequent run it goes red, this stops us getting notified when there are blips with servers that actually aren’t an issue.

Anyway, all sorted now. Thanks anyway.

1 Like