我的目标是创建一个可调度的 PowerShell 脚本,用于报告最近注册的设备。我已经创建了应用程序并授予了一些权限。
$OauthTokenEndpoint = 'https://login.microsoftonline.com/tenantid/oauth2/token'
$OauthRequest = @{
grant_type="client_credentials"
client_id = "clientidguid"
client_secret = "clientidsecret"
resource = "https://graph.microsoft.com"
scope="DeviceManagementManagedDevices.Read.All"
}
$AuthResponse = Invoke-RestMethod -Uri $OauthTokenEndpoint -Method Post -ContentType application/x-www-form-urlencoded -Body $OauthRequest
$Token = $authresponse.access_token
#this query completes successfully
$Success = Invoke-restmethod -uri https://graph.microsoft.com/v1.0/users/username@domain.com/ownedDevices -Headers @{Authorization = "Bearer $Token"} -method Get
#this query fails with 401 unauthorised
$401Error = Invoke-RestMethod -Headers @{Authorization = "Bearer $Token"} -uri "https://graph.microsoft.com/beta/managedDevices/deviceguid?`$select=hardwareInformation" -Method GET
我相信我的问题是我没有或不能授予我的应用程序的 DeviceManagementManagedDevices.Read.All 范围权限。此 API 可与 Graph Explorer 一起使用,我有此脚本的交互式版本,它使用有效的委派权限。如何允许我的应用程序访问 ManagedDevices API 端点,以便我可以非交互方式使用它。