SOLVED

need help with script

%3CLINGO-SUB%20id%3D%22lingo-sub-2658353%22%20slang%3D%22en-US%22%3Eneed%20help%20with%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2658353%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20all%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20mimic%20the%20behavior%20of%20a%20dynamic%20group.%20This%20is%20for%20on-prem%20AD.%26nbsp%3B%20This%20is%20what%20i%20have%20to%20do.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%231%20check%20two%20OU's%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CDIV%3E%3CDIV%3E%3CDIV%3E%3CDIV%3E%3CDIV%3EIf%20user%20live%20in%20the%20Corp%5CConsultants%5C*%20or%20Corp%5CFM%20Users%5CCognizant%5C*%20OU%20locations%2C%20then%20add%20the%20users%20to%20group%20%22Example1%22%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%232%20if%20users%20are%20not%20found%20in%26nbsp%3BCorp%5CConsultants%5C*%20or%20Corp%5CFM%20Users%5CCognizant%5C*%20OU%20locations%2C%20then%20remove%20the%20users%20that%20are%20not%20found%20from%20the%20%22Example1%22%20group%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EAny%20help%20is%20greatly%20appreciated%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2658353%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20Phone%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20Server%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2659094%22%20slang%3D%22en-US%22%3ERe%3A%20need%20help%20with%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2659094%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1096811%22%20target%3D%22_blank%22%3E%40Skipster311-1%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24x%3DGet-ADUser%20-SearchBase%20'DC%3DTest%2CDC%3Dlocal'%20-Filter%20*%20%0Aforeach(%24s%20in%20%24x)%7B%0Aif%20((%24s.distinguishedName%20-like%20%22*OU%3DConsultants%2CDC%3DTest%2CDC%3Dlocal%22)%20-or%20(%24s.distinguishedName%20-like%20%22*OU%3DFM%20Users%2CDC%3DTest%2CDC%3Dlocal%22))%7BWrite-Host%20%22He%20is%20OK%22%7D%0Aelse%7B%0A%24s.distinguishedName%0ARemove-ADGroupMember%20-Identity%20Example1%20-Members%20%24s%20-Confirm%3A%24false%0A%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20this%20works%2C%20please%20mark%20the%20question%20as%20the%20best%20respone.%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2661343%22%20slang%3D%22en-US%22%3ERe%3A%20need%20help%20with%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2661343%22%20slang%3D%22en-US%22%3Eyour%20script%20was%20absolutely%20fantastic!%20Thank%20you%20again%3C%2FLINGO-BODY%3E
Frequent Contributor

Hello all

 

I am trying to mimic the behavior of a dynamic group. This is for on-prem AD.  This is what i have to do.

 

#1 check two OU's 

If user live in the Corp\Consultants\* or Corp\FM Users\Cognizant\* OU locations, then add the users to group "Example1"
 
#2 if users are not found in Corp\Consultants\* or Corp\FM Users\Cognizant\* OU locations, then remove the users that are not found from the "Example1" group
 
Any help is greatly appreciated
6 Replies

@Skipster311-1 

$x=Get-ADUser -SearchBase 'DC=Test,DC=local' -Filter * 
foreach($s in $x){
if (($s.distinguishedName -like "*OU=Consultants,DC=Test,DC=local") -or ($s.distinguishedName -like "*OU=FM Users,DC=Test,DC=local")){Write-Host "He is OK"}
else{
$s.distinguishedName
Remove-ADGroupMember -Identity Example1 -Members $s -Confirm:$false
}
}

 

If this works, please mark the question as the best respone.

Thanks

 

best response confirmed by Skipster311-1 (Frequent Contributor)
Solution

@Skipster311-1 I would do it like this. It looks complex, but you need to take into account that you want to identify users to be removed as well. So we first gather all group members and expect them all to be removed. Then we check both OUs and if a user in those OUs is present in the group, set their action to do nothing, and if a user is not present yet, it's obviously a new user so should be added. This then leaves all the rest set to be removed. At the end we then do the actual action.

 

This was quick and dirty, not optimized, and may contain mistakes ;) It could be optimized to do the OU checks in a function etc to avoid code duplication.

 

$GroupMembers = @{}
ForEach ($User in Get-ADGroupMember -Identity Example1) {
    $GroupMembers.Add($User.DistinguishedName, @{"DN"=$User.DistinguishedName;"Action"="Remove"})
}

ForEach ($User in Get-ADUser -SearchBase "OU=Consultants,DC=Test,DC=local" -Filter *) {
    if (!$GroupMembers[$User.DistinguishedName]) {
        # User found in OU, but not in group yet --> Add user
        $GroupMembers.Add($User.DistinguishedName, @{"DN"=$User.DistinguishedName;"Action"="Add"})
    } else {
        # User found in OU, and also present in group --> Do Nothing
        $GroupMembers[$User.DistinguishedName].Action = "Nothing"
    }
}

ForEach ($User in Get-ADUser -SearchBase "OU=FM Users,DC=Test,DC=local" -Filter *) {
    if (!$GroupMembers[$User.DistinguishedName]) {
        # User found in OU, but not in group yet --> Add user
        $GroupMembers.Add($User.DistinguishedName, @{"DN"=$User.DistinguishedName;"Action"="Add"})
    } else {
        # User found in OU, and also present in group --> Do Nothing
        $GroupMembers[$User.DistinguishedName].Action = "Nothing"
    }
}

ForEach ($User in $GroupMembers.Values) {
    if ($User.Action -eq "Add") {
        Write-Host "Adding $($User.DN) to group"
        Add-ADGroupMember -Identity Example1 -Members $User.DN
    }
    if ($User.Action -eq "Remove") {
        Write-Host "Removing $($User.DN) from group"
        Remove-ADGroupMember -Identity Example1 -Members $User.DN -Confirm:$false
    }
}

 

your script was absolutely fantastic! Thank you again
I want to thank you for responding to my post. Alot of people dont take the time to do that. I was able to get what i needed from @pvanberlo suggestion. Thank you again for your help
You're very welcome! Just drop me a note if there's anything else I can help with :)
Thank you