header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Get-ADUser filter multiple OU searchbase?
Last Post 01 Sep 2010 11:36 AM by bpc. 1 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
bpcUser is Offline
New Member
New Member
Posts:2
Avatar

--
31 Aug 2010 08:16 AM
    I have written the script below and it works, except that I need to be able to either specify certain OUs to search or exclude certain OUs from being searched. I know I can use the -searchbase parameter on the filter, but as far as I know, that will only allow me to specify one OU to be searched. The other option I was thinking of was adding a "where" statement and specifying specific distinguished names, but cannot figure out the syntax for that. Here is the code, thank you for your help:
    $EmlUsers = Get-ADuser -property mail -Filter {mail -like "*@example.com"} | foreach-object {$_.samaccountname} $ExampleClients = Get-ADGroupMember -Identity ExampleGroup | ForEach-Object {$_.samaccountname} ForEach ($username in $EmlUsers) { If($ExampleClients -notcontains $username) { Add-ADGroupMember -Identity ExampleGroup $username -Confirm:$false } elseif ($ExampleClients -contains $username) { Write-Output "$username is already a member of this group" } } forEach ($GroupMember in $ExampleClients) { If ($EmlUsers -notcontains $GroupMember) { Remove-ADGroupMember -Identity ExampleGroup $GroupMember -Confirm:$false } }
    bpcUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    01 Sep 2010 11:36 AM
    I got it working, this might not be the best way, but here is the code in case this helps anybody else. I basically just created a separate array for each OU that I wanted to search, then I combined the arrays. I also used the 'Where' statement and wildcards to ignore certain Child OUs: $ADQ1 = Get-ADuser -Filter {mail -like "*@example.com"} -SearchBase "OU=Dept1,DC=Example,DC=NET" | Where-Object {$_.DistinguishedName -notlike "*OU=Example1,OU=Example1,OU=Dept1,DC=Example,DC=net" -and $_.DistinguishedName -notlike "*OU=Example2,OU=Example2,OU=Dept1,DC=Example,DC=net"} | foreach-object {$_.samaccountname} $ADQ2 = Get-ADuser -Filter {mail -like "*@Example.com"} -SearchBase "OU=Dept2,DC=Example,DC=NET" | Where-Object {$_.DistinguishedName -notlike "*OU=Example,OU=Example,OU=Dept2,DC=Example,DC=net" -and $_.DistinguishedName -notlike "*OU=Example2,OU=Example2,OU=Dept2,DC=Example,DC=net"} | foreach-object {$_.samaccountname} $EmlUsers = $ADQ1 + $ADQ2
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer