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