top of page
Writer's picturePhilipp Hiestand

Exception Handling with powershell's Invoke-WebRequest


Stick guy jumping between the challenge of using REST API's

REST API's are the status quo for connecting different applications and leveraging multiple features into one platform. There are many automation engines around but sometimes I face the situation were you need to combine those actions into a simple middleware. In the world of an #Microsoft engineer you probably use #powershell. In my second blog post I introduced the way I structure powershell code into classes and still to today it helps me make my powershell code much easier to use. Next up I'll show you how to work with HTTP Status codes. First you'll start with an Invoke-WebRequest where you skip the httperrorcheck so you'll be able to continue you're script flow without being stopped by an error pop up.

$this.convertContentToObject(
	(
	Invoke-WebRequest -Uri $url -Headers $headers -Body $body 
	-ContentType $contentType -Method $method 
	-skipHttpErrorCheck
	), $url
)

The Invoke-WebRequest contains the normal parameters like uri, headers, body, contentType, method plus the magic skipHttpErrorCode (only possible in Powershell 7.x). So what you see additionally is that I wrapped the whole command in a method from object class called convertContentToObject. In this method I will catch the error code and run it through multiple validations before returning the response back.

[PSCustomObject]convertContentToObject($response, $url){
        try {
            $this.validation($response, $url)
            return $response.Content | ConvertFrom-Json
        } catch {
            Write-host "$(Get-Date) [Warning] .......................::::::::::::::::"
            Write-host "$(Get-Date) [Warning] $($PSitem)"
            Write-host "$(Get-Date) [Warning] $($_.ScriptStackTrace)"
            Write-host "$(Get-Date) [Warning] $($_.TargetObject)"
            Write-host "$(Get-Date) [Warning] $($_.InvocationInfo)"
            Write-host "$(Get-Date) [Warning] $($_.Exception)"
            Write-host "$(Get-Date) [Warning] .......................::::::::::::::::"
            return $null
        }
    }

In the convertContentToObject method I'll take the response and run through the validation method as for logging purposes I also insert back the $url. If an error occured till now I would catch and log it.

In the validation method you'll free to catch any HTTP Code or any other necessity given by you're REST API.

validation($value, $url){
          if ($value.StatusCode -ne 200){
            	"$(Get-Date) [Warning] $url returned $($value.StatusCode)" |
 			Add-Content -Path $Global:logFile
        }
}

My findings are skipping the http error code at the Invoke-WebRequest and handling it separately allows for more options during exception handling. Additionally this version is written in the object-oriented programming style which helps structure the code better and reuse it multiple times.




109 views0 comments

Recent Posts

See All

©2024 philipphiestand.ch
My blog is about technology, security, sustainability and my ideas to combine those elements into digital solutions.

bottom of page