I created a PS script to add ADusers to a group if Email attribute contains "abc.com" or "def.com" and existing users in the group will skip the process. Below is my script
import-module ActiveDirectory
$Users = Get-ADUser -Filter * -Properties emailaddress
$existingUsers = Get-ADgroupmember "Test_group"
foreach ($user in $Users) {
#if user existing in the group, Skip this process
if (($existingUsers | Where-Object { $_.sAMAccountName -eq $user.sAMAccountName }) -eq $null) {
#if user email attribute contain "abc.com" or "def.com"
if ($user.emailaddress -match "abc.com" -or $user.emailaddress -match "def.com") {
$GroupMembers = Get-ADGroupMember -Identity "test_group" | Select -ExpandProperty SamAccountName
if ($User.SamAccountName -NotContains $GroupMembers) {
Add-ADGroupMember -Identity "test_group" -Members $User
}
}
}
}
However, after execution, there is 2 return error as below.
I added "add key="MaxGroupOrMemberEntries" value="200000"
in ADWS config but issue persist.
Get-ADGroupMember : Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory Web Services running. At C:\scripts\AutoAddUserToGroup.ps1:15 char:50 + $GroupMembers = Get-ADGroupMember <<<< -Identity "test_group" | Select -ExpandProperty SamAccountName + CategoryInfo : ResourceUnavailable: (test_group:ADGroup) [Get-ADGroupMember], ADServerDownException + FullyQualifiedErrorId : Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory Web Servic es running.,Microsoft.ActiveDirectory.Management.Commands.GetADGroupMember
Add-ADGroupMember : The specified account name is already a member of the group At C:\scripts\AutoAddUserToGroup.ps1:18 char:34 + Add-ADGroupMember <<<< -Identity "test_group" -Members $User + CategoryInfo : NotSpecified: (test_group:ADGroup) [Add-ADGroupMember], ADException + FullyQualifiedErrorId : The specified account name is already a member of the group,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember