Search and add filtered users to group

First time poster, and still very new to POSH.

I’m trying to create a script to run as a scheduled task to filter users not part of a given group within a scope of OUs, and add them to the filtered group within AD.

Here’s what I have written so far:

## Group filter ## $groupname = (Get-ADGroup 'group1').DistinguishedName ## ## SearchBase filter ## $OUs = "OU=OU1,DC=domain,DC=local","OU=OU2,DC=domain,DC=local" ## ## Users filter ## $newmembers = Foreach ($OU in $OUs) { Get-ADUser -Filter { not (memberof -eq $groupname)} -SearchBase $OU } ## ## Add users to group ## Add-ADGroupMember -Identity $groupname -Members $newmembers 

I get a return error of “Add-ADGroupMember : The specified account name is already a member of the group” which leads me to believe the users filter is not working properly, however removing the final line and the $newmembers variable and testing the filter seems to filter the users as intended. Any suggestions? Any help is much appreciated.

pastebin: http://pastebin.com/D9EazvmF

submitted by /u/Phoenixeye0
[link] [comments]

Leave a Reply