top of page

Exception Handling with powershell's Invoke-WebRequest

Writer's picture: Philipp HiestandPhilipp Hiestand

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.




222 views0 comments

Recent Posts

See All

Avoiding Management Deadlocks

In your professional life, you may encounter decision blocks where two managers shift responsibility from one person to another. As a...

AI China vs AI USA

The information war is reaching new heights. With new leadership in America and globalization being tested (Tariff War), we see the...

Monopolistic tactics and deleting data

It's been now more than 7 months, several emails and multiple phone calls since my first interaction to delete my data on Meta products....

Comments


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

bottom of page