[CmdletBinding()] param( [string]$Url = $env:MONITOR_WEBHOOK_URL, [Parameter(Mandatory = $true)] [string]$Message, [ValidateSet('info', 'warning', 'critical')] [string]$Severity = 'warning', [int]$TimeoutSec = 10, [switch]$Send, [switch]$Json ) $ErrorActionPreference = 'Stop' $payload = [ordered]@{ source = $env:COMPUTERNAME severity = $Severity message = $Message timestamp = (Get-Date).ToUniversalTime().ToString('o') } $body = $payload | ConvertTo-Json -Depth 4 if (-not $Send) { Write-Host 'DRY RUN: webhook not sent' Write-Output $body exit 0 } if ([string]::IsNullOrWhiteSpace($Url)) { Write-Host 'ERROR: set MONITOR_WEBHOOK_URL or pass -Url' exit 2 } try { $response = Invoke-RestMethod -Uri $Url -Method Post -ContentType 'application/json' -Body $body -TimeoutSec $TimeoutSec -ErrorAction Stop Write-Host ("Webhook sent to {0}" -f $Url) if ($null -ne $response) { if ($Json) { $response | ConvertTo-Json -Depth 4 } else { Write-Output $response } } exit 0 } catch { Write-Host ("ERROR: {0}" -f $_.Exception.Message) exit 2 }