Good morning,
While working with a script to create a group in Domain A, then add groups from Domain B and C to the initial group throws an error. However I noticed if I wait a time I can add the group without error.
To verify the new group was created I input a do\while loop with a try catch and it verifies the group is created, adds group from domain b to domain A then fails on group from domain c if I add too quickly.
New-ADGroup -Name $DomainBRoleGroup -GroupScope Global -GroupCategory Security -Path $DomainBRoleGroupPath -Server $DomainB
$valDomainBRoleGroup = Get-ADGroup -Filter {SamAccountName -eq $DomainBRoleGroup } -Properties samAccountName -Server $DomainB
New-ADGroup -Name $DomainCFunctionalGroup -GroupScope Universal -GroupCategory Security -Path $DomainCFunctionalGroupPath -Server $DomainC
$valDomainCFunctionalGroup = Get-ADGroup -Filter {SamAccountName -eq $DomainCFunctionalGroup } -Server $DomainC
If (Get-ADGroup -Filter {SamAccountName -eq $siteListGroup} -Server $DomainA) {
$a = new-object -comobject wscript.shell
$b = $a.popup(“The Group $siteListGroup already exists and will not be created, exiting script! “,0,”Message From DomainA Script”,1)
Break
}Else{
New-ADGroup -Name $siteListGroup -GroupScope DomainLocal -GroupCategory Security -Path $DomainAResourceGroupPath -Server $DomainA
}
$ValsiteListGroup = Get-ADGroup $siteListGroup -Server $DomainA
Add-ADGroupMember -Identity $valSiteListGroup -Members $vaDomainBRoleGroup -ErrorAction Stop
Add-ADGroupMember -Identity $valSiteListGroup -Members $valDomainCFunctionalGroup -ErrorAction Stop
Add-ADGroupMember : The specified group type is invalid
At line:10 char:5
I run both add-adgroupmember back to back it errors DomainBRoleGroup but adds DomainCfunctionalGroup, then if I wait a random amount of time and go then run the add-adgroupmember line again for domain it works fine.
I have tried a do/while loops with a try catch doing a match on the sid of DomainB as a member in DomainA list group but it doesn’t work.
I am lost because the do/while works great for domainC to DomainA any time but DomainB (which is a child domain of A) only works when it likes.
Any suggestions would be greatly appreciated