Score:0

Invoke-RestMethod : The remote server returned an error: (400) Bad Request for PATCH request

in flag

I am working on stale device cleanup script to perform cleanup of stale devices from Azure AD tenant. I am also using graphAPI to retrieve device information Via GET request which works fine. But when I try to use PATCH/POST request to update device status, it is not working as expected and throws an 400 error message. I have tried modifying the body part and received 404 and 403 error messages as well. Can someone please help with the below function ?

$AzureToken = Get-MsalToken -TenantId $azureTenantId -ClientId $azureAplicationId -ClientSecret $azurePassword

$authheader = @{    
"Authorization" = "Bearer $($AzureToken.AccessToken)"
"Content-type" = "application/json"    
}


$ssoPatchUri = 'https://graph.microsoft.com/v1.0/devices/#############’

$body = @{ "extensionAttributes"= @{
                                "extensionAttribute6"= "$((Get-Date).ToFileTimeUtc())"
                            }
                            "accountEnabled"= $false
                        }
                                 
                        
Invoke-RestMethod -Headers $authheader -Uri $ssoPatchUri -Method PATCH -Body ($body| ConvertTo-Json -Depth 5)

Error Message:

Invoke-RestMethod : The remote server returned an error: (404) Not Found. At line:1 char:1

  • Invoke-RestMethod -Headers $authheader -Uri $ssoPatchUri -Method PATC ...
  •   + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
      + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
    
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.