Created
October 11, 2018 14:32
-
-
Save mdavis332/75dc76978826e823d5c5c3bc6edb5382 to your computer and use it in GitHub Desktop.
Get the email addresses of group members using a CN path and ldap query.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$SaveLocation = "$home\Desktop\emails.csv" | |
Write-Output 'Querying for email addresses of all group members. This may take a few minutes...' | |
$Searcher = New-Object DirectoryServices.DirectorySearcher | |
$Filter = '(&(objectCategory=person)(ObjectClass=user)(|(memberOf:1.2.840.113556.1.4.1941:=CN=AllFaculty,OU=Staff Faculty,DC=domain,DC=local)(memberOf:1.2.840.113556.1.4.1941:=CN=Allstaff,OU=Staff Faculty,DC=domain,DC=local)(memberOf:1.2.840.113556.1.4.1941:=CN=AllAdjunct,OU=Staff Faculty,DC=domain,DC=local))(!(userAccountControl:1.2.840.113556.1.4.803:=2)))' | |
$Searcher.Filter = $Filter | |
$Searcher.SearchRoot = 'LDAP://DC=domain,DC=local' | |
$Users = $Searcher.FindAll() | |
$Emails = @() | |
foreach ($Result in $Users) { | |
if ($Result.Properties.mail -ne $null -and $Result.Properties.mail -ne '') { | |
$Obj = New-Object PSObject | |
$Obj | Add-Member -MemberType NoteProperty -Name email -Value ([string]$Result.Properties.mail.Trim()) | |
$Emails += $Obj | |
} | |
} | |
if ($Emails.count -gt 0) { | |
Write-Output "Found $($Emails.count) emails. Saving them to $SaveLocation" | |
$Emails | Export-Csv $SaveLocation -NoTypeInformation | |
} else { | |
Write-Output "Found 0 emails. An error probably occurred." | |
} | |
Write-Host -NoNewLine 'Press any key to continue...' | |
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment