I'm working on a script to create Security and Distribution groups so that they will follow our naming standards. In the script I use New-DistributionGroup to create the group, Set-DistributionGroup to add some Custom attributes, and then want to use Set-ADGroup to add a Description. (aside: what a pain that New-DistributionGroup can't do these things!) Despite setting sleep times up to 30 seconds, Set-ADGroup always fails with:
Set-ADGroup : Cannot find an object with identity: 'AcctTesting1' under: [our domain].
Yet if I remove the line of code from Set-ADGroup, run the script, and then immediately run the Set-ADGroup code, it works perfectly.
Here is the relevant portion of the script: (our domain name removed)
$GroupTypeName = "Assignment"
$OU = $BaseOU + "AssignmentGroups"
New-DistributionGroup -Name $SAMname -Alias $Alias -DisplayName $DisplayName -ManagedBy `
"CN=Administrator,CN=Users,[our domain]" -OrganizationalUnit $OU `
-SamAccountName $SAMname -Type Security
Set-DistributionGroup -Identity $SAMname -CustomAttribute10 "ASSIGNMENT GROUP" `
-CustomAttribute11 $PRMCode
echo "Waiting for new group to replicate"
Start-Sleep -s 20
Set-ADGroup -Identity $SAMname -Description "$Alias AssignmentGroup"
I'm relatively new to PowerShell scripting, so if you have ideas on how to fix this, I'd appreciate a little detail!