Private/Get-MailFromDN.ps1
function Get-MailFromDN { <# .SYNOPSIS Retrieves the email address (mail attribute) of a user from their DN. .PARAMETER DN The distinguished name (DN) of the user. .OUTPUTS The mail address (string), or $null if not found or on error. #> [CmdletBinding()] param( [Parameter(Mandatory, ValueFromPipeline, Position = 0)] [ValidateNotNullOrEmpty()] [string]$DN ) process { try { $user = [ADSI]("LDAP://$DN") return $user.Properties["mail"][0] } catch { Write-Warning "Unable to retrieve mail for $DN : $($_.Exception.Message)" return $null } } } |