Powershell Invoke-RestMethod error 400

Rick Angel 91 Reputation points
2022-05-05T17:39:08.81+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,390 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 30,386 Reputation points Microsoft Vendor
    2022-05-23T07:22:54.723+00:00

    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.

    0 comments No comments