Identity/Identity.ps1

<#
.SYNOPSIS
    Short description
.DESCRIPTION
    Long description
.EXAMPLE
    PS C:> <example usage>
    Explanation of what the example does
.INPUTS
    Inputs (if any)
.OUTPUTS
    Output (if any)
.NOTES
    General notes
#>

Function Get-TfsIdentity
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Position=0,Mandatory=$true)]
        [object]
        $Identity,

        [Parameter()]
        [switch]
        $QueryMembership,

        [Parameter(ValueFromPipeline=$true)]
        [object]
        $Collection
    )

    Process
    {
        if ($Identity -is [Microsoft.VisualStudio.Services.Identity.Identity]) { _Log "Input item is of type Microsoft.VisualStudio.Services.Identity.Identity; returning input item immediately, without further processing."; return $Identity }

        $tpc = Get-TfsTeamProjectCollection -Collection $Collection; if (-not $tpc -or ($tpc.Count -ne 1)) {throw "Invalid or non-existent team project collection $Collection."}
        
        $client = _GetRestClient 'Microsoft.VisualStudio.Services.Identity.Client.IdentityHttpClient' -Collection $tpc

        if($QueryMembership.IsPresent)
        {
            $qm = [Microsoft.VisualStudio.Services.Identity.QueryMembership]::Direct
        }
        else
        {
            $qm = [Microsoft.VisualStudio.Services.Identity.QueryMembership]::None
        }

        if(_TestGuid $Identity)
        {
            _Log "Finding identity with ID [$Identity] and QueryMembership=$qm"
            $task = $client.ReadIdentityAsync([guid]$Identity); $result = $task.Result; if($task.IsFaulted) { _throw "Error retrieving information from identity [$Identity]" $task.Exception.InnerExceptions }
        }
        else
        {
            _Log "Finding identity with account name [$Identity] and QueryMembership=$qm"
            $task = $client.ReadIdentitiesAsync([Microsoft.VisualStudio.Services.Identity.IdentitySearchFilter]::AccountName, [string]$Identity, 'None', $qm); $result = $task.Result; if($task.IsFaulted) { _throw "Error retrieving information from identity [$Identity]" $task.Exception.InnerExceptions }
        }

        return $result
    }
}