functions/utility/Resolve-Identity.ps1
function Resolve-Identity { [CmdletBinding()] param ( [Parameter(Mandatory = $true, ParameterSetName = 'ByName', ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string[]] $Name, # Must include: UserName, UserSid, ComputerName, IsDomainAccount - one of UserName or UserSid may be empty [Parameter(Mandatory = $true, ParameterSetName = 'ByObject', DontShow = $true)] $InputObject, [switch] $AsHashtable, [string] $Server, [pscredential] $Credential ) begin { if ($AsHashtable) { $identityHash = @{ } } $adParameters = $PSBoundParameters | ConvertTo-PSFHashtable -Include Server, Credential } process { #region InputObject if ($InputObject) { Add-Member -InputObject $InputObject -MemberType NoteProperty -Name IdentityData -Value $null -Force $identityName = $InputObject.UserSid if (-not $identityName) { $identityName = $InputObject.UserName } if (-not $identityName) { Stop-PSFFunction -Message "Unable to resolve identity of input object - at least one property out of 'Username','UserSid' must be present. Ensure the correct data has been provided: $InputObject" -Target $InputObject -EnableException $true -Cmdlet $PSCmdlet -Category InvalidArgument } try { $result = Get-PrincipalInformation @adParameters -Identity $identityName } catch { throw } if ($InputObject.PSObject.Properties.Name -contains 'UserSid') { $InputObject.UserSid = $result.ObjectSID } if (-not $AsHashtable) { return $result } $identityHash[$result.SamAccountName] = $result } #endregion InputObject #region Name foreach ($principalName in $Name) { try { $result = Get-PrincipalInformation @adParameters -Identity $principalName } catch { Write-Error $_ continue } if (-not $AsHashtable) {return $result } $identityHash[$result.SamAccountName] = $result } #endregion Name } end { if ($AsHashtable) { $identityHash } } } |