在Azure PowerShell中添加组成员

inn6fuwd  于 2023-03-30  发布在  Shell
关注(0)|答案(1)|浏览(156)

我试图写一个脚本来添加一个组中的PowerShell成员,我写的波纹管脚本似乎对我来说是正确的我它不工作。请帮助

$grpname = Read-Host "Please enter group name:"
   $group = Get-AzureADGroup -filter "DisplayName eq '$grpname'"
   $groupId = $group.ObjectId

   $job = Read-Host "Please enter Job title:"
   $userjob = Get-AzADUser -filter "Jobtitle eq '$job'"
   $userId = $userjob.Id

   foreach ($userId in $userIds) {
   Add-AzureADGroupMember -ObjectId $groupId -RefObjectId $userId
   }
   Write-Output " You can see bellow members of ($grpname) : "
   Get-AzureADGroupMember -ObjectId "$groupId"
8yoxcaq7

8yoxcaq71#

除了Halfix在他们的评论中提到的之外,你的代码没有什么特别的问题,唯一可能发生的事情是没有发现用户的jobTitle等于$job中的用户输入。
另外,您应该将代码迁移到Microsoft.Graph Module,因为AzureAD Module很快就会被弃用:

下面是使用Graph Cmdlet的代码外观:

$grpname = Read-Host "Please enter group name"
$group = Get-MgGroup -Filter "DisplayName eq '$grpname'"

if(-not $group) {
    return "No group found with displayName '$grpName'"
}

if($group.Count -gt 1) {
    return "Multiple groups found with displayName '$grpName'"
}

$job = Read-Host "Please enter Job title"
$users = Get-MgUser -Filter "jobTitle eq '$job'"

if(-not $users) {
    return "No users found with jobTitle '$job'"
}

foreach($user in $users) {
    New-MgGroupMember -GroupId $group.Id -DirectoryObjectId $user.Id
}

"You can see bellow members of ($grpname):"
Get-MgGroupMember -GroupId $group.Id

执行此操作需要的权限为GroupMember.ReadWrite.All和User.ReadWrite.All,请参见Graph API文档中的权限部分。

相关问题