Logging heartbeats from PowerShell

The Heartbeats feature is a great way to verify that scripts run successfully too. A lot of people have PowerShell scripts running on a schedule to clean up folders on the file system, make batch changes in a database, and more.

To include heartbeats in your PowerShell script, wrap the code in try/catch and add either Healthy or Unhealthy result:

$apiKey = "API_KEY"
$logId = "LOG_ID"
$heartbeatId = "HEARTBEAT_ID"
$url = "https://api.elmah.io/v3/heartbeats/$logId/$heartbeatId/?api_key=$apiKey"

try
{
    # Your script goes here

    $body = @{
        result = "Healthy"
    }
    Invoke-RestMethod -Method Post -Uri $url -Body ($body|ConvertTo-Json) -ContentType "application/json-patch+json"
}
catch
{
    $body = @{
        result = "Unhealthy"
        reason = $_.Exception.Message
    }
    Invoke-RestMethod -Method Post -Uri $url -Body ($body|ConvertTo-Json) -ContentType "application/json-patch+json"
}

If everything goes well, a Healthy heartbeat is logged using the Invoke-RestMethod cmdlet. If an exception is thrown in your script, an Unhealthy heartbeat is logged.


This article was brought to you by the elmah.io team. elmah.io is the best error management system for .NET web applications. We monitor your website, alert you when errors start happening, and help you fix errors fast.

See how we can help you monitor your website for crashes Monitor your website