我正在開發一個 PowerShell 腳本,以根據組(安全組)為用戶分配 Office 365 許可證。因此,我創建了應用注冊并分配了所需的 API 權限。
當我嘗試運行我的腳本時,我收到以下錯誤
Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At line:1 char:1
Invoke-RestMethod -Uri $uri -Body $body -ContentType "application/jso ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
下面是整個腳本
$connectiondetails = @{
# This ids and secret are present in the overview and certificate & secret page of our application in azure AD
# Tenant ID here
'tenantid' = ""
# Application (client) ID here
'clientid' = ""
# Secret id here
'ClientSecret' = "" | ConvertTo-SecureString -AsPlainText -Force
}
$token = Get-MsalToken @connectiondetails
$tokenid_ = $token.AccessToken
# $uri = "https://graph.microsoft.com/v1.0/groups"
# $grp = Invoke-RestMethod -Uri $uri -Headers @{Authorization=("bearer {0}" -f $tokenid_)}
# $grp
$uri = "https://graph.microsoft.com/v1.0/groups/ffbabc6f-aa87-40f3-8665-9d140e4a7adb/assignLicense"
$body = "{""SkuId"":""cbdc14ab-d96c-4c30-b9f4-6ada7cdc1d46""}"
# assign license call
Invoke-RestMethod -Uri $uri -Body $body -ContentType "application/json" -Method post -Headers @{Authorization=("bearer {0}" -f $tokenid_)}
分配給應用的權限

我需要幫助才能知道做錯了什么。謝謝你。
嘗試的解決方案

uj5u.com熱心網友回復:
添加許可證的請求正文需要addLicenses具有權限Group.ReadWrite.All和Directory.ReadWrite.All.
{
"addLicenses": [
{
"skuId": "cbdc14ab-d96c-4c30-b9f4-6ada7cdc1d46"
}
],
"removeLicenses": []
}
附言
$uri = "https://graph.microsoft.com/v1.0/groups/ffbabc6f-aa87-40f3-8665-9d140e4a7adb/assignLicense"
# create json object
$data = @{
"addLicenses" = @(
@{
"skuId" = "cbdc14ab-d96c-4c30-b9f4-6ada7cdc1d46"
}
)
"removeLicenses" = @()
}
# convert to JSON-formatted string
$body = $data | ConvertTo-Json
# assign license call
Invoke-RestMethod -Uri $uri -Body $body -ContentType "application/json" -Method post -Headers @{Authorization=("bearer {0}" -f $tokenid_)}
資源:
組 - 分配許可證
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/528427.html
