This is the script I am using and is falling with error: Add-ADGroupMember : The server is unwilling to process the request
Import AD Module
Import-Module ActiveDirectory
Import the data from CSV file and assign it to variable
$List = Import-Csv "C:\Temp\Test.csv"
Foreach ($User in $List) {
# Retrieve UserSamAccountName and ADGroup
$UserSam = $User.SamAccountName
$Groups = $User.Group
# Retrieve SamAccountName and ADGroup
$ADUser = Get-ADUser -Filter "SamAccountName -eq '$UserSam'" -Server "fqdn of GC:3268" | Select-Object SamAccountName
$ADGroups = Get-ADGroup -Filter * | Select-Object Name
# User does not exist in AD
if ($ADUser -eq $null) {
Write-Host "$UserSam does not exist in AD" -ForegroundColor Red
Continue
}
# User does not have a group specified in CSV file
if ($Groups -eq $null) {
Write-Host "$UserSam has no group specified in CSV file" -ForegroundColor Yellow
Continue
}
# Retrieve AD user group membership
$ExistingGroups = Get-ADPrincipalGroupMembership $UserSam | Select-Object Name
foreach ($Group in $Groups.Split(';')) {
# Group does not exist in AD
if ($ADGroups.Name -notcontains $Group) {
Write-Host "$Group group does not exist in AD" -ForegroundColor Red
Continue
}
# User already member of group
if ($ExistingGroups.Name -eq $Group) {
Write-Host "$UserSam already exists in group $Group" -ForeGroundColor Yellow
}
else {
# Add user to group
Add-ADGroupMember -Identity $Group -Members $UserSam -Server "fqdn of GC:3268"
Write-Host "Added $UserSam to $Group" -ForeGroundColor Green
}
}
}