Error Handling for Invoke-RestMethod – Powershell

This is somewhat awkward but the only way to do it as far as I know without doing something more complicated like using .NET’s WebRequest and ConvertFrom-Json (or whatever data format you are expecting).

try {
    Invoke-RestMethod ... your parameters here ... 
} catch {
    # Dig into the exception to get the Response details.
    # Note that value__ is not a typo.
    Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ 
    Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)