12Knocksinna / Office365itpros

Office 365 for IT Pros PowerShell examples
MIT License
1.24k stars 559 forks source link

Getting (400) Bad Request Error Here #79

Closed ghost closed 1 year ago

ghost commented 1 year ago

I’ve tried running this script but getting this error. Could you please help me resolve it.

Accessing the Graph to get user sign-in data... Invoke-RestMethod: The remote server returned an error: (400) Bad Request. At Cr Users \admin\Desktop\lastsignin. ps1:31 char :16

IMG_6113

Could you please be able to help me resolve it.

replyonly commented 1 year ago

The script contained an ‘ before $top=999 on line 30. I removed this ' and the script ran successfully.

apgoncalves commented 1 year ago

I´m getting the same error. The problem is, if I remove de backtick, $select and $top is ignored.

12Knocksinna commented 1 year ago

This is an old script (3 years) and I wouldn't use this approach to get sign in data for users anymore.

https://office365itpros.com/2022/09/29/underused-accounts-report/ explains how to use the Microsoft Graph PowerShell SDK cmdlet Get-MgUser to fetch information about users (including signins) with a command like:

[Array]$Users = Get-MgUser -Filter "assignedLicenses/`$count ne 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable Records -All -Property signInActivity | Sort-Object DisplayName

I think that's a better approach.

Anyway, the reason why the call is failing is that the Azure AD team has limited fetches to 120 objects at a time. That's a recent change:

{"error":{"code":"UnknownError","message":"{\"@odata.context\":\"http://reportingservice.act ivedirectory.windowsazure.com/auditLogs/$metadata#Edm.String\",\"value\":\"Cannot query data for more than 120 users at a time.\"}","innerError":{"date":"2023-04-01T09:43:21","requ est-id":"c51d7e23-0469-405d-9170-4206e25af69d","client-request-id":"c51d7e23-0469-405d-9170- 4206e25af69d"}}}

Change the $top=999 to $top=100 and the command will run. The backtick is necessary to escape the $ character in PowerShell.