Microsoft Graph g-raph (giraffe) - the spirit animal of Microsoft Graph

How to add AdditionalHeaders to Invoke-PnPGraphMethod?

2 min read.

This article explains how to easily add additional headers to the Graph API calls you make in PowerShell using Invoke-PnPGraphMethod commandlet. There’s a convenient sample for you to copy-paste and some basic explanations on what we’re doing below.

Background

Every now and then, I need to make some Graph API calls from PowerShell. And there’s a convenient PowerShell commandlet available to make it easy to do just that – Invoke-PnPGraphMethod.

But to make some of the calls successful, you need to add some additional headers. Yes, “consistencyLevel: eventual”, I’m looking at you.

Again, Invoke-PnPGraphMethod is our friend. It has an additional parameter called “AdditionalHeaders” that accepts a string dictionary of headers – we just need to know the right way to add them.

But how do you do that with PowerShell?

Solution

Let’s not beat around the bush. The solution is very straightforward, and here it is in copy-pasteable format:

$url = "https://graph.microsoft.com/beta/users?"

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("consistencyLevel", "eventual");

$result = Invoke-PnPGraphMethod -Url $url -Method "Get" -AdditionalHeaders $headers

… and because search engines need to be satisfied, here’s a bloated how-to on, well, how to do this and why:

Time needed: 2 minutes

How to supply Additional Headers (like consistencyLevel: eventual) to Invoke-PnPGraphMethod PowerShell commandlet?

  1. Create a string Dictionary object to hold our additional headers

    Create a new instance of the System.Collections.Generic.Dictionary class and assigned it to the variable $headers.

    This dictionary will be used to store additional headers that need to be included in the API request.

  2. Add your desired headers to the Dictionary

    Now we can add our headers. I’m going with a true classic – consistencyLevel: eventual. See below.

    $headers.Add("consistencyLevel", "eventual")

    This line adds a key-value pair to the $headers dictionary. The key "consistencyLevel" is associated with the value "eventual". These headers are used to provide additional information or configuration options for the API request. In this case, it sets the consistency level to “eventual,” indicating that eventual consistency is acceptable for the response data.

    This is required for a lot of Graph API methods.

  3. And finally, call the Graph

    Now we are ready to execute our Graph call!

    Invoke-PnPGraphMethod -Url $url -Method "Get" -AdditionalHeaders $headers

    This line invokes the Invoke-PnPGraphMethod function from the PnP PowerShell module. It sends an HTTP GET request to the specified URL ($url) with the provided additional headers ($headers).

This code sets up the necessary parameters and headers and then makes a request to the Microsoft Graph API to retrieve user data using the beta version of the API.

mm
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments