azure 使用MsGraph分配许可证

bq3bfh9z  于 2023-03-19  发布在  其他
关注(0)|答案(2)|浏览(184)

我需要使用MsGraph分配许可证-如果您在这里,您知道它将在三月底过时。
到目前为止,我找到了这个。$添加许可证= @(@{SkuID = $license DisabledPlans = $DisabledPlans})设置-管理用户许可证-用户ID $upn -添加许可证$添加许可证-删除许可证@()其中:$license仅为AccountSkuId,因为您不再需要该域。在本例中,$DisabledPlans为MFA_PREMIUM(针对EMSPREMIUM许可证进行测试)
我得到的错误:**设置管理员用户许可证:无法将文本“EMSPREMIUM”转换为预期类型“Edm. Guid”。**位于第5行,字符数:15

  • ...设置-管理用户许可-用户ID $upn -添加许可$添加许可-...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • 类别信息:无效操作:({用户ID = jneb... ionJsonSchema }:〈〉f__匿名类型8 2) [Set-MgUserLicense_AssignExpanded1], RestException 1
  • 完全限定错误ID:我认为问题出在$许可证变量上。

提前感谢您的帮助!

9fkzdhlc

9fkzdhlc1#

我尝试在我的环境中重现相同的结果,如下所示

感谢@jdweng的建议。SkuPartNumber = "EMSPREMIUM"
我已使用以下PowerShell代码将EMS E5许可证分配给用户。

Install-Module -Name Microsoft.Graph.Authentication -Force
Import-Module -Name Microsoft.Graph.Authentication -Force
Connect-MgGraph -Scopes User.ReadWrite.All, Organization.Read.All
$upn           = "upnname"
$EMSE5 = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'EMSPREMIUM'
 Set-MgUserLicense -UserId $upn -AddLicenses @{SkuId = $EMSE5.SkuId} -RemoveLicenses @()

输出:

当我在门户中检查相同内容时,已成功将许可证分配给用户。

有关使用Msgraph PowerShell分配许可证的详细信息,请参阅:Assigning licenses to user accounts

beq87vna

beq87vna2#

你好,如果你需要使用rest API请求而不是cmdlet,下面是我将如何分配一个用户office365许可证。

# your app registration details
$tenantId = ""
$appId = ""
$appSecret = ""

$authUrl = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
$body = @{
    client_id     = $appId
    scope         = "https://graph.microsoft.com/.default"
    client_secret = $appSecret
    grant_type    = "client_credentials"
}
$response = Invoke-RestMethod -Method POST -Uri $authUrl -Body $body
$accessToken = $response.access_token

# user email address to assign the license
$userId = "user@yourdomain.com"

# you need to know the [license SKUID][1]
# api get request for licenses can be returned using
# $apiUrl = "https://graph.microsoft.com/v1.0/subscribedSkus"
$licenseSkuId = ""
$apiUrl = "https://graph.microsoft.com/v1.0/users/$userId/assignLicense"
$headers = @{
    "Authorization" = "Bearer $accessToken"
    "Content-Type" = "application/json"
}

# Set license assignment properties
$data = @{
    "addLicenses" = @(
        @{
            "skuId" = $licenseSkuId
        }
    )
    "removeLicenses" = @()
}

$body = $data | ConvertTo-Json

# API request
Invoke-RestMethod -Method post -Uri $apiUrl -Headers $headers -Body $body

# Check response
if ($response -ne $null) {
    Write-Output "License assigned successfully"
} else {
    Write-Output "Failed to assign license"
}

相关问题