Reputation: 13
I'm looking to learn some powershell since not a lot of people in my organisation know a lot about coding. I just started using PS and it was a long time since i wrote any code whatsoever so I'm just trying to do a simple script that gets all the group members from one Azure AD group and adds them to another. Been looking around and taking bits from here and there and this is what i got so far.
$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
The errors i am getting is:
$currentuser = Get-AzureADUser -ObjectId $member | select obj ... Add-AzureADGroupMember : Cannot bind argument to parameter 'RefObjectId' because it is null.
Upvotes: 1
Views: 3593
Reputation: 1
For groups with over 100 users:
$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
}
Upvotes: 0
Reputation: 42133
I can reproduce your issue on my side, you need to add .ObjectId
in your command.
$currentuser = Get-AzureADUser -ObjectId $member.ObjectId | select objectid
Also you need to make sure the member in $group1
is not in $group2
, otherwise you will get a One or more added object references already exist
error.
Upvotes: 1