我希望学习一些Powershell,因为组织中没有多少人对编码了解很多。我刚刚开始使用PS,因为我编写了任何代码都已经有很长时间了,所以我只是在尝试做一个简单的脚本,将一个Azure AD组中的所有组成员添加到另一个。一直四处张望,从这里到那里取些东西,这就是我到目前为止所得到的。
$Credential = Get-Credential
Connect-AzureAD -Credential $Credential
$group1 = "ObjectId pulled from AAD"
$group2 = "ObjectId pulled from AAD"
$membersGroup1 = Get-AzureADGroupMember -ObjectId $group1
foreach($member in $membersGroup1)
{
$currentuser = Get-AzureADUser -ObjectId $member | select objectid
Add-AzureADGroupMember -ObjectId $group2 -RefObjectId $currentuser.objectid
}
Disconnect-AzureAD
我遇到的错误是:
$ currentuser = Get-AzureADUser -ObjectId $ member |选择obj ... Add-AzureADGroupMember:无法将参数绑定到参数 “ RefObjectId”,因为它为null。
答案 0 :(得分:1)
我可以在我这边重现您的问题,您需要在命令中添加.ObjectId
。
$currentuser = Get-AzureADUser -ObjectId $member.ObjectId | select objectid
还需要确保$group1
中的成员不在$group2
中,否则会出现One or more added object references already exist
错误。
答案 1 :(得分:0)
对于超过 100 个用户的群组:
$group1 = "ObjectId pulled from AAD"
$group2 = "ObjectId pulled from AAD"
$membersGroup1 = Get-AzureADGroupMember -ObjectId $group1 -All $true
foreach($member in $membersGroup1)
{
$currentuser = Get-AzureADUser -ObjectId $member.ObjectId | select objectid
Add-AzureADGroupMember -ObjectId $group2 -RefObjectId $currentuser.objectid
}