Freigeben über


Code snippet: What account does Team Foundation see me authenticated as?

Brian posted this snippet in the forums (needs to reference Microsoft.TeamFoundation.Client.dll, should be in the GAC on installed clients) to use the properties off the TeamFoundationServer instance to show which account and friendly name Team Foundation sees you as.

 using System;
using Microsoft.TeamFoundation.Client;

namespace TfsWhoAmI
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1 || args[0] == "/?" || args[0] == "-?")
            {
                Console.WriteLine("Copyright(c) Microsoft Corporation");
                Console.WriteLine();
                Console.WriteLine("Usage: TfsWhoAmI <server>");
                Environment.Exit(0);
            }

            try
            {
                TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(args[0]);
                Console.WriteLine("Current user: {0} ({1})", 
                                  tfs.AuthenticatedUserDisplayName, 
                                  tfs.AuthenticatedUserName);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
                Environment.Exit(1);
            }
        }
    }
}

A simple (no error checking or usage info) Monad version would just select those properties off the instance, of course:

function tfs_who_am_i($server)
{
    [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($server) |
        select AuthenticatedUserDisplayName, AuthenticatedUserName
}

MSH C:\Documents and Settings\jmanning> tfs_who_am_i("jmanning-test")

AuthenticatedUserDisplayName AuthenticatedUserName
---------------------------- ---------------------
James Manning NORTHAMERICA\jmanning