question

RickAngel-4713 avatar image
0 Votes"
RickAngel-4713 asked IanXue-MSFT answered

Powershell Invoke-RestMethod error 400

I’ve been looking for a script to identify stale Azure AD users. Trying to run the example at https://github.com/azure365pro/AzureAD_Inactive_Users/blob/main/AzureAD_Inactive_Users.ps1. Does it work in a GCC High tenant? I’ve modified the URLs to reference the “.us” suffix but getting an error when the script runs the line below.

$ExpiredUsers = (Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $LoginUrl -Method Get).value

The error message is:

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At C:\xxx\Documents\Scripts\AzureAD_Inactive_Users.ps1:58 char:18
+ ... redUsers = (Invoke-RestMethod -Headers @{Authorization = "Bearer $($t ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

Hi,

Please make sure the URL you are trying to access is valid. You can also try to add the "Content-Type" header to the headers.

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.