Private/Common/Resolve-PukIdentity.ps1
|
function Resolve-PukIdentity { [CmdletBinding()] [OutputType([string])] param( [Parameter(Mandatory)] [string]$Identity, [string]$Server ) Import-PukActiveDirectoryModule $commonParams = @{ ErrorAction = 'Stop' } if ($Server) { $commonParams.Server = $Server } try { $user = Get-ADUser -Identity $Identity @commonParams return $user.DistinguishedName } catch { # Get-ADUser -Identity does not resolve UserPrincipalName; fall back to a filter lookup. if ($Identity -notmatch '@') { throw "Unable to resolve identity '$Identity' in Active Directory: $($_.Exception.Message)" } } $escapedIdentity = $Identity.Replace("'", "''") try { $user = Get-ADUser -Filter "UserPrincipalName -eq '$escapedIdentity'" @commonParams } catch { throw "Unable to resolve identity '$Identity' in Active Directory: $($_.Exception.Message)" } if (-not $user) { throw "Unable to resolve identity '$Identity' in Active Directory: no matching user." } return $user.DistinguishedName } |