远程服务器返回错误:(403)禁止PowerShell

avwztpqn  于 9个月前  发布在  Shell
关注(0)|答案(1)|浏览(142)

我想通过从PowerShell调用MS Graph来获取Azure AD组的display namecreatedDateTime
为此,我使用下面的PS脚本:

$Body = @{
    client_id = "app_id"
    client_secret = "secret"
    scope = "https://graph.microsoft.com/.default"
    grant_type = 'client_credentials'
}

$Connect_Graph = Invoke-RestMethod -Uri "https://login.microsoftonline.com/my_tenant_id/oauth2/v2.0/token" -Method Post -Body $Body

$token = $Connect_Graph.access_token

$query = "https://graph.microsoft.com/v1.0/groups/"
$groups = (Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $query -Method Get).value | Select displayName, createdDateTime

字符串
失败,错误代码为403 Forbidden

Invoke-RestMethod : The remote server returned an error: (403) Forbidden.
At C:\Users\script.ps1:13 char:12
+ $groups = (Invoke-RestMethod -Headers @{Authorization = "Bearer $($to ...
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand


我已授予Group.Read.AllDirectory.Read.All的权限.

dw1jzc5e

dw1jzc5e1#

请检查您授予**Group.Read.AllDirectory.Read.All**的权限类型。

  • 如果您尝试以登录用户身份访问API,则必须使用Delegated权限。
  • 如果您尝试在没有登录用户的情况下访问API,则必须使用Application权限。
  • 我在我的环境中执行了相同的脚本,当我有委派权限而没有登录用户时,得到了相同的错误,如下所示:*


的数据

为了解决错误,我为**Group.Read.AllDirectory.Read.All授予了应用权限**,并执行了以下脚本:

$Body = @{
    client_id = "app_id"
    client_secret = "secret"
    scope = "https://graph.microsoft.com/.default"
    grant_type = 'client_credentials'
}
$Connect_Graph = Invoke-RestMethod -Uri "https://login.microsoftonline.com/my_tenant_id/oauth2/v2.0/token" -Method Post -Body $Body
$token = $Connect_Graph.access_token
$query = "https://graph.microsoft.com/v1.0/groups/"
(Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $query -Method Get).value | Select displayName, createdDateTime

字符串

我成功地得到了如下结果:


相关问题