0
votes

I have an MS Graph API in PowerShell working for the most part.

I am using

$Uri = $null
$Uri = "https://graph.microsoft.com/v1.0/users?$select=displayName,givenName,postalCode"
$payload=$null
$payload = Invoke-RestMethod -uri $Uri  -Headers $Header -Method Get -ContentType "application/json"
$payload.value

however, it is not changing the field selection. It keeps returning the default fields as demonstrated here

https://docs.microsoft.com/en-us/graph/api/user-list?view=graph-rest-1.0&tabs=http

What could I possibly be doing wrong?

I am using application based authentication. The payload is being returned but it is not recognizing the $select statement.

No errors are being returned by the PowerShell

I run it in Graph Explorer it works fine.

1

1 Answers

0
votes

The error is caused by the type of string declaration used for the Uri string. You are declaring the string like this:

$Uri = "https://graph.microsoft.com/v1.0/users?$select=displayName,givenName,postalCode"

This tells Powershell, that you want to evaluate the string. $ is Powershell's variable identifier. Undeclared variables are set automatically to an empty string, when evaluated in a string. Therefore the request executed against the Graph Api is:

https://graph.microsoft.com/v1.0/users?=displayName,givenName,postalCode

Your can check this yourself by writing the variable to the host:

Write-Host $Uri

If you execute this query with the Graph Explorer. It will return all users without an applied filter, which is the behaviour you have observed. You need to change the declaration to:

$Uri = 'https://graph.microsoft.com/v1.0/users?$select=displayName,givenName,postalCode'

Then, Powershell will not interpret $select as a variable and your request should work properly.