Microsoft Graph - PowerShell错误返回Get-MgUserMemberOf组成员



祝你新年快乐,身体健康。我正在将一些PowerShell脚本从Msol/AzureAD更新到Graph,因为我知道前者很快就会被弃用。我有一个根据(A)AD组成员关系分配365个许可证,但出现错误。脚本的相关部分如下:

$UnlicensedUsers = Get-MgUser -Filter 'assignedLicenses/$count eq 0' -ConsistencyLevel eventual -CountVariable unlicensedUserCount -All | Select Id
ForEach ($User in $UnlicensedUsers)
{
$Groups = Get-MgUserMemberOf -UserID $User
if ($Groups -match $NoLicence)
{
} elseif ($Groups -match $A3Staff) {
if ($Groups -match $GroupA) {
Update-MgUser -UserId $User -UsageLocation GB
Set-MgUserLicense -UserId $User -AddLicenses $StaffA3SKU -LicenseOptions $StaffA3Options
}
}
}

得到的错误是:

Get-MgUserMemberOf : Resource 'Microsoft.Graph.PowerShell.Models.MicrosoftGraphUser' does not exist or one of its queried reference-property objects are not present.
At line:1 char:1
+ Get-MgUserMemberOf -UserID $User
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: ({ UserId = Micr...ndProperty =  }:<>f__AnonymousType5`10) [Get-MgUserMemberOf_List], RestException`1
+ FullyQualifiedErrorId : Request_ResourceNotFound,Microsoft.Graph.PowerShell.Cmdlets.GetMgUserMemberOf_List

我连接到图形:Connect-MgGraph -Scopes User.ReadWrite。Organization.Read。, Directory.Read.All

我要做的是Get-MgUser返回未经许可的用户,然后Get-MgUserMemberOf返回每个组的所有成员关系。然后检查前面定义的各种组,并为其分配不同的许可/选项。我在应用程序中添加了Directory.Read.All权限,导入了Microsoft.Graph.Users模块。我不知道我哪里出错了。我认为Get-MgUser返回的对象可能不正确。如有任何建议,不胜感激,并提前表示感谢。

p。我注意到带有-Property Id的Get-MgUser返回:@{Id=}而不仅仅是。

Import-Module Microsoft.Graph.Users连接- mggraph -范围用户读写。Organization.Read。所有,Directory.Read.All

已更新/同意应用程序权限

Graph做的事情完全不同,但我能够通过下面的更改来破解:

$StaffSKU = Get-MgSubscribedSku -All | Where SkuPartNumber -eq '<SKUName>'
$StaffOptions = $StaffSKU.ServicePlans | where ServicePlanName -in ("DisabledPlan1","DisabledPlan2") | Select -ExpandProperty ServicePlanId
$UnlicensedUsers = Get-MgUser -Filter 'assignedLicenses/$count eq 0' -ConsistencyLevel eventual -CountVariable unlicensedUserCount -All
ForEach ($User in $UnlicensedUsers)
{
$Groups = Get-MgUserMemberOf -UserID $User.Id
if ($Groups.Id -match $NoLicence)
{
} elseif ($Groups.Id -match $Staff) {
if ($Groups.Id -match $GroupA) {
$AddLicenses = @(
@{SkuId = $StaffSKU.SkuId
DisabledPlans = $StaffOptions
}
)
Update-MgUser -UserId $User.Id -UsageLocation GB
Set-MgUserLicense -UserId $User.Id -AddLicenses $AddLicenses -RemoveLicenses @()
}

使用Graph API调用,您必须返回SKU对象,不能仅通过名称甚至ID来处理它。尽管Get-MgUser返回一个用户对象,Get-MgUserMemberOf只需要Update-MgUser和Set-MgUserLicense也是如此。

最新更新