Skip to content

Instantly share code, notes, and snippets.

@gioxx
Created September 28, 2018 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gioxx/1843f68762a17aeb0bfb4cb50768e1e6 to your computer and use it in GitHub Desktop.
Save gioxx/1843f68762a17aeb0bfb4cb50768e1e6 to your computer and use it in GitHub Desktop.
Lo script popola un gruppo di Active Directory inserendo al suo interno tutti gli utenti che trova all'interno di una OU specificata. Maggiori dettagli disponibili nell'articolo sul blog: https://wp.me/pdQ5q-9On
Import-Module ActiveDirectory
$groupname = "CN=CONTOSO_Dipendenti,OU=Contoso,OU=Gruppi,DC=contoso,DC=local"
$users = Get-ADUser -Filter * -SearchBase "OU=Contoso,OU=Utenti,DC=contoso,DC=local"
foreach($user in $users)
{
Add-ADGroupMember -Identity $groupname -Member $user.samaccountname -ErrorAction SilentlyContinue
}
$members = Get-ADGroupMember -Identity $groupname
foreach($member in $members)
{
if($member.distinguishedname -notlike "*OU=Contoso,OU=Utenti,DC=contoso,DC=local*")
{
Remove-ADGroupMember -Identity $groupname -Member $member.samaccountname
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment