How can I get my if statement conditions to work properly using the Orion SDK with PowerShell?
For whatever reason, I can only get one of the conditions to match no matter what I do.
Is there something I may be missing for the if statements? I basically have the SWQL Queries as the conditions and then have it execute either disabling or enabling the alert.
If I change the queries or results, it doesn't seem to change either. Maybe there is a simpler way to do it?
# Setup Connection to SWIS
$SwisCreds = Get-Credential -Message "enter your Orion Creds"
$SwisConnection = Connect-Swis -Hostname "XXXXX" -Credential $SwisCreds
# SWQL Queries for servers up or down
$Down = "SELECT Caption AS NODE
FROM Orion.Nodes
WHERE Caption like 'XXX%' AND Status = '2'
$Up = "SELECT Caption AS NODE
FROM Orion.Nodes
WHERE Caption like 'XXX%' AND Status = '1'
# Check to see if alerting has been enabled or disabled
$Enabled = "SELECT AlertID, Name, Enabled
FROM Orion.AlertConfigurations
where AlertID = 'X' and Enabled = 'TRUE'
$Disabled= "SELECT AlertID, Name, Enabled
FROM Orion.AlertConfigurations
where AlertID = 'X' and Enabled != 'TRUE'
# Query SWIS with the states of the servers being either up or down
$ResultsDown = Get-SwisData -SwisConnection $SwisConnection -Query $Down
$ResultsUp = Get-SwisData -SwisConnection $SwisConnection -Query $Up
$Check_Alerts_Enabled = Get-SwisData -SwisConnection $SwisConnection -Query $Enabled
$Check_Alerts_Disabled = Get-SwisData -SwisConnection $SwisConnection -Query $Disabled
# Check to see if alerting has been enabled or disabled
# Disable or Enable Alerts due to matching conditions, if no results match write-host
if ($ResultsDown -and $Check_Alerts_Enabled) {
Get-SwisData $SwisConnection "SELECT uri FROM Orion.AlertConfigurations where AlertID = 'X' or AlertID = 'X'" | Set-SwisObject $SwisConnection -Properties @{Enabled=$false}
Write-Host "One or more polling Engine is down, disabling Alerts.";break
}
elseif ($ResultsUp -and $Check_Alerts_Disabled) {
Get-SwisData $SwisConnection "SELECT uri FROM Orion.AlertConfigurations where AlertID = 'X' or AlertID = 'X'" | Set-SwisObject $SwisConnection -Properties @{Enabled=$true}
write-host "All polling engines are back up, enabling alerts.";break
}
else {
Write-Host "No changes have been detected, nothing to do."
}