get-memberships.ps1 - programmatically finding the groups you're a member of

Forum thread was asking for this (admittedly, in C#).  Since it's less typing in PowerShell, that's what I'll post instead :)  It'll be in the next update to the PowerShell scripts, along with a version of get-tfs.ps1 that figures out the tfs instance based on your current dir if it's mapped.

While I would assume you just want the "FullDisplayName" from this, I go the route of add-property to make it easier on those that may want to still see the SID, or the project uri, or the SecurityGroup bool, or whatever.  It defaults to just looking at the groups you're a direct member of, but with "expanded" scope you can look at groups you're both a direct and indirect member of.

Some example output from our dogfood server:

 C:\# get-memberships northamerica\jmanning tkbgitvstfat01 | ft -a fulldisplayname,type

FullDisplayName                                              Type
---------------                                              ----
NORTHAMERICA\Department 73868                        WindowsGroup
REDMOND\Team Foundation Server Terminal Svr User     WindowsGroup
REDMOND\DevDiv Feature Directory Users               WindowsGroup
REDMOND\VSTSSG                                       WindowsGroup
Team Foundation Valid Users                      ApplicationGroup


C:\# get-memberships northamerica\jmanning tkbgitvstfat01 expanded | ft -a fulldisplayname,type

FullDisplayName                                              Type
---------------                                              ----
NORTHAMERICA\Department 73868                        WindowsGroup
REDMOND\Team Foundation Server Terminal Svr User     WindowsGroup
REDMOND\BurtonAll                                    WindowsGroup
REDMOND\BurtonSG                                     WindowsGroup
REDMOND\DevDiv Feature Directory Users               WindowsGroup
REDMOND\DevDivAll                                    WindowsGroup
REDMOND\VSTSSG                                       WindowsGroup
Team Foundation Valid Users                      ApplicationGroup
[Devdiv Project Management]\Contributors         ApplicationGroup
[VS Express]\Readers                             ApplicationGroup
[Devdiv Planning]\Contributors                   ApplicationGroup
[Devdiv Tasks]\Contributors                      ApplicationGroup
[Developer Division Product Scope]\Contributors  ApplicationGroup
[VSTS V2 Plans]\Contributor                      ApplicationGroup
[Personal]\Contributors                          ApplicationGroup
[VSTS Dogfood]\Contributor                       ApplicationGroup
[MQ]\Contributors                                ApplicationGroup
[Devdiv Content and Intl]\Work Item Contributors ApplicationGroup
[Orcas]\Contributors                             ApplicationGroup 

Here ya go:

 param (
    [string] $accountName = $(throw 'accountName is needed'),
    [string] $server = '.', # defaults to finding tfs by current dir
    [string] $membershipType = 'direct' # defaults to just direct memberships
)

$tfs = get-tfs $server

function translateDomainToFullDisplayName(
    [string] $domainName,
    [string] $displayName
)
{
    # if it's empty, leave it alone (global identities)
    if (!$domainName) { return $displayName }

    # if it's a vstfs uri, use CSS to translate it to a team project prefix
    if ($domainName -match '^vstfs://')
    {
        $projectName = $tfs.css.GetProject($domainName).Name
        return "[$projectName]\$displayName"
    }

    # all others, just prefix it
    return "$domainName\$displayName"
}


$memberOfSids = $tfs.gss.ReadIdentity('accountName', $accountName, $membershipType).memberof

$memberOfIdentities = $tfs.gss.Readidentities('sid', $memberOfSids, 'none')

$memberOfIdentities = $memberOfIdentities | %{
    add-member -in $_ noteproperty FullDisplayName (
        translateDomainToFullDisplayName $_.Domain $_.DisplayName
    ) -passthru
}

$memberOfIdentities