Private/Get-SamAccountNameFromDN.ps1
function Get-SamAccountNameFromDN { <# .SYNOPSIS Retrieves samAccountName for a user when you already have the DN. .PARAMETER DN Distinguished name of the user object. .OUTPUTS samAccountName (string) or $null if not found / on error. #> [CmdletBinding()] param( [Parameter(Mandatory, ValueFromPipeline, Position = 0)] [ValidateNotNullOrEmpty()] [string]$DN ) process { try { $user = [ADSI]("LDAP://$DN") return $user.Properties["samAccountName"][0] } catch { Write-Warning "Unable to read samAccountName for $DN : $($_.Exception.Message)" return $null } } } |