[CmdletBinding()] param( [string[]]$Url = @('https://example.com'), [int]$TimeoutSec = 10, [int[]]$ExpectStatus = @(200, 204), [switch]$Json ) $ErrorActionPreference = 'Stop' $results = @() foreach ($item in $Url) { $watch = [System.Diagnostics.Stopwatch]::StartNew() try { $response = Invoke-WebRequest -Uri $item -Method Get -TimeoutSec $TimeoutSec -UseBasicParsing -ErrorAction Stop $watch.Stop() $code = [int]$response.StatusCode $results += [pscustomobject]@{ Url = $item Status = $code Milliseconds = [math]::Round($watch.Elapsed.TotalMilliseconds) Ok = ($ExpectStatus -contains $code) Error = '' } } catch { $watch.Stop() $code = 0 if ($null -ne $_.Exception.Response) { $code = [int]$_.Exception.Response.StatusCode } $results += [pscustomobject]@{ Url = $item Status = $code Milliseconds = [math]::Round($watch.Elapsed.TotalMilliseconds) Ok = $false Error = $_.Exception.Message } } } if ($Json) { $results | ConvertTo-Json -Depth 3 } else { $results | Format-Table -AutoSize } $failed = @($results | Where-Object { -not $_.Ok }) if ($failed.Count -gt 0) { Write-Host ("Smoke test failed: {0} of {1}" -f $failed.Count, $results.Count) exit 1 } Write-Host ("Smoke test passed: {0} endpoints" -f $results.Count) exit 0