medinibster
medinibster

Reputation: 57

check uri content with powershell and alert for string match

I am executing this code:

$web = Invoke-WebRequest http://x.x.x.x:60210/CoreApi/api/Healthcheck
$web.tostring()

The response in $web is as below.

HealthStatus:DBConnectionSuccess:True EventStoreConnectionSuccess:True UnpublishedEvents:0 AzureBusConnectionSuccess:True Errors:NONE

I need to create an alert for condition UnpublishedEvents:[>10]. Can someone help me with just the string match logic.

Upvotes: 0

Views: 289

Answers (2)

Daniel Brixen
Daniel Brixen

Reputation: 1663

Here is a basic solution using a regular expression:

$output = 'HealthStatus:DBConnectionSuccess:True EventStoreConnectionSuccess:True UnpublishedEvents:11 AzureBusConnectionSuccess:True Errors:NONE'

$unpublishedEvents = 0

$regex = 'UnpublishedEvents:([0-9]+)'

$match = [regex]::match($output, $regex)
if ($match.Success)
{
  $unpublishedEvents = $match.Groups[1].Value
}

if ($unpublishedEvents -gt 10)
{
  Write-Host "Some alert! ($unpublishedEvents events)"
}

Upvotes: 1

TToni
TToni

Reputation: 9391

You can use a regular expression with a named capture group to do that, like this:

$input = "HealthStatus:DBConnectionSuccess:True EventStoreConnectionSuccess:True UnpublishedEvents:20 AzureBusConnectionSuccess:True Errors:NONE"
$isMatch = $input -match "UnpublishedEvents:(?<UnpubEventCount>\d+)"
if ($isMatch)
{
    return $Matches.UnpubEventCount -gt 10
}
else
{
    Write-Error "UnpublishedEvents not found"
}

$Matches is a "magic" variable that is set when using the -match operator.

Upvotes: 2

Related Questions