11 Responses

  1. David Pasek
    David Pasek at |

    I think that programmers generally believe that try/catch mechanism is more elegant. That’s IMHO true for bigger projects where exception can fall through the code and you can catch it later in the code. However I agree with you that for small scripts it would be much cleaner to simply test return status. Everything depends on point of view and particular use case 🙂

  2. Tackling Basic RESTful Authentication with PowerShell - Wahl Network

    […] 64-74 are where the API call occur using Invoke-WebRequest. As I’ve mentioned in earlier posts, I prefer this cmdlet over Invoke-RestMethod because it returns the entire response, not just the […]

  3. Using Try and Catch to Handle External Errors - Wahl Network

    […] while back, I wrote a post entitled Using Try and Catch with PowerShell’s Invoke-WebRequest as I explored handling errors with PowerShell. Since then, I’ve learned other ways it can be […]

  4. Juha Pakkanen
    Juha Pakkanen at |

    There is actually a quite elegant way and common way of catching e.g. return values from a webrequest and that is the -ErrorVariable parameter. Depends of course how the service processes and returns it’s responses.

  5. Marc
    Marc at |

    While I appreciate your approach, I can’t see how Failure could work – indeed it’s not working for me (PS 3.0 Strict). $body is not visible to Failure, $moref is undefined and you are not passing $_ to Failure.

  6. Invoke-RestMethod Exception Handling with Tintri APIs | Tintri API and UI Ramblings

    […] that error information in the response?  Fortunately Chris Wahl shared some great information on his blog.  As you can see, the first three bold lines in the second part of  the Catch block is directly […]

  7. Using the GET Method with PowerShell and RESTful APIs - Wahl Network

    […] I wrap the Invoke-WebRequest cmdlet within a Try / Catch. If you’re new to this, go read my Using Try and Catch with PowerShell’s Invoke-WebRequest post. The response is saved to a throw-away variable (meaning I often re-use it later) named $r. […]

  8. Mircea Coste
    Mircea Coste at |

    I ended up doing this(thanks for the idea)
    try {
    $url = Invoke-WebRequest -Uri ‘http://www.google.com/test’ -Headers $Headers
    if($url.StatusCode -eq 200){
    Write-Host “StatusCode:” $url.StatusCode -fore Green
    Write-Host “StatusDescription:” $url.StatusCode -fore Green
    Write-Host “StatusCode:” $url.StatusCode -fore Yellow
    Write-Host “StatusDescription:” $url.StatusCode -fore Yellow
    } catch {
    #run an IF statement to see if we have an exception or we get a valid response.
    #if ($_.Exception.Message -eq 1){
    # Dig into the exception to get the Response details.
    # Note that value__ is not a typo.
    Write-Host “StatusCode:” $_.Exception.Response.StatusCode.value__ -fore Red
    Write-Host “StatusDescription:” $_.Exception.Response.StatusDescription -fore Red

  9. RogerM
    RogerM at |

    Chris, Mircea, thank you. My script does API calls via invoke-webrequest, and the target device reboots during the process a few times. Pinging the device is no good, because a positive ping doesn’t mean I can complete and API call. So I was looking for a way to do the API call over and over, until I got a code 200, and ignoring all the failures. This helped a lot. Here is what I did:

    # API test function
    function TestAPIconnection {
    param($devip, $key)
    $x = 1
    $totalTimes = 1000
    $cverURL = “https://” + $devip + “//api/?key=” + $Key + “&type=version”

    while($x -lt 1001){

    #Generate a progress bar showing percentage done – visual and frivolous
    $percentComplete = ($x / $totalTimes) * 100
    Write-Progress -Activity ‘Connecting’ -Status “Trying to connect to $devip $x times” -PercentComplete $percentComplete

    try {
    $tver = (Invoke-WebRequest -Uri $cverURL -TimeoutSec 60)
    if($tver.StatusCode -eq 200){
    return “0”

    } catch {
    if ($_.Exception.Message -eq 1){
    # Note that value__ is not a typo.
    $thisVariable = “I-DoNotCare”
    if ($x -eq 1000) {
    return “1”}

    # Run the function as follows

    $result = (TestAPIconnection -devip $myIP -key $apikey)

    if ($MMresult -eq “0” ) {
    Write-host “Connected to $myIP”}

    if ($MMresult -eq “1” ) {
    Write-host “Cannot connect to $myIP. Exiting …”

  10. Roger
    Roger at |

    $result = (TestAPIconnection -devip $myIP -key $apikey)

    should be:

    $MMresult = (TestAPIconnection -devip $myIP -key $apikey)


Share your point of view!