Created
July 26, 2017 10:43
-
-
Save Webbanditten/bd477b9dca820bfe32d2593131bc7892 to your computer and use it in GitHub Desktop.
List Project administrator and collection administrators on TFS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Microsoft.TeamFoundation.Framework.Common; | |
using Microsoft.TeamFoundation.Client; | |
using Microsoft.TeamFoundation.Framework.Client; | |
using System.Collections.Generic; | |
using Microsoft.TeamFoundation.Core.WebApi; | |
using Microsoft.TeamFoundation.Server; | |
using ProjectInfo = Microsoft.TeamFoundation.Server.ProjectInfo; | |
namespace TFSUserManager | |
{ | |
class Program | |
{ | |
static TfsConfigurationServer _server; | |
static List<string> _users = new List<string>(); | |
static void Main(string[] args) | |
{ | |
var url = "SERVER URL"; | |
var amount = 0; | |
_server = TfsConfigurationServerFactory.GetConfigurationServer(new Uri(url)); | |
_server.Connect(ConnectOptions.IncludeServices); | |
Log($"Connected to {url}", ConsoleColor.Green); | |
var collectionNodes = _server.CatalogNode.QueryChildren(new[] { CatalogResourceTypes.ProjectCollection }, false, CatalogQueryOptions.None); | |
foreach (var collectionNode in collectionNodes) | |
{ | |
Uri uri = new Uri(url + "/" + collectionNode.Resource.DisplayName); | |
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(uri); | |
//var collection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(uri); | |
tfs.EnsureAuthenticated(); | |
TfsTeamProjectCollection teamProjectCollection = _server.GetTeamProjectCollection(tfs.InstanceId); | |
teamProjectCollection.Authenticate(); | |
var structureService = (ICommonStructureService)teamProjectCollection.GetService(typeof(ICommonStructureService)); | |
IGroupSecurityService2 gss = (IGroupSecurityService2)teamProjectCollection.GetService(typeof(IGroupSecurityService2)); | |
var collectionAdministrators = gss.ReadIdentity(SearchFactor.AccountName, "Project Collection Administrators", QueryMembership.Direct); | |
foreach (var collectionAdminId in collectionAdministrators.Members) | |
{ | |
Identity m = gss.ReadIdentity(SearchFactor.Sid, collectionAdminId, QueryMembership.None); | |
//Log($"Found collection Admin {m.AccountName}"); | |
if (m.SecurityGroup == false) | |
{ | |
AddUser(m.AccountName); | |
} | |
} | |
var projectInfoList = new List<ProjectInfo>(structureService.ListAllProjects()); | |
foreach (ProjectInfo pi in projectInfoList) | |
{ | |
// ProjectInfo gives you the team project's name, status, and URI. | |
var teamProjectName = pi.Name; | |
var teamProjectState = pi.Status; | |
var teamProjectUri = pi.Uri; | |
// print team project info | |
/*Console.WriteLine("------------------------------------------------------------"); | |
Console.WriteLine("Project Name : {0}", teamProjectName); | |
Console.WriteLine("Project Status : {0}", teamProjectState); | |
Console.WriteLine("Project Uri : {0}", teamProjectUri);*/ | |
Console.WriteLine("Loading users from: {0}", teamProjectName); | |
// skip this team project if it is not WellFormed. | |
if (teamProjectState != ProjectState.WellFormed) | |
{ | |
continue; | |
} | |
// Requirement 2: For each Team project, I need to identify each security Group | |
Identity[] allProjectGroups = gss.ListApplicationGroups(teamProjectUri); | |
// iterate thru the project group list and get a list of direct members for each project group | |
foreach (Identity projectGroup in allProjectGroups) | |
{ | |
// Requirement 3: For each Security Group, I need to identify all the users that belong to that group | |
Identity pg = gss.ReadIdentity(SearchFactor.Sid, projectGroup.Sid, QueryMembership.Direct); | |
/*Console.WriteLine(" --------------------------------------------------------"); | |
Console.WriteLine(" Project Group Name : {0}", pg.DisplayName);*/ | |
if (pg.DisplayName == "Project Administrators") | |
{ | |
// print detailed identity info for each group member | |
foreach (String groupMemberSid in pg.Members) | |
{ | |
Identity m = gss.ReadIdentity(SearchFactor.Sid, groupMemberSid, QueryMembership.None); | |
if (m.SecurityGroup == false) | |
{ | |
/*Console.WriteLine(" ----------------------------------------------------"); | |
Console.WriteLine(" AccountName : {0}", m.AccountName); | |
Console.WriteLine(" Deleted : {0}", m.Deleted); | |
Console.WriteLine(" Description : {0}", m.Description); | |
Console.WriteLine(" DisplayName : {0}", m.DisplayName); | |
Console.WriteLine(" DistinguishedName : {0}", m.DistinguishedName); | |
Console.WriteLine(" Domain : {0}", m.Domain); | |
Console.WriteLine(" MailAddress : {0}", m.MailAddress); | |
Console.WriteLine(" SecurityGroup : {0}", m.SecurityGroup); | |
Console.WriteLine(" Sid : {0}", m.Sid); | |
Console.WriteLine(" SpecialType : {0}", m.SpecialType); | |
Console.WriteLine(" IdentityType : {0}", m.Type);*/ | |
AddUser(m.AccountName); | |
} | |
} | |
} | |
} | |
} | |
} | |
Log($"Amount of users: {_users.Count}"); | |
foreach (var user in _users) | |
{ | |
Log(user); | |
} | |
Console.ReadLine(); | |
} | |
private static void AddUser(string accountName) | |
{ | |
if (!_users.Contains(accountName) && !(accountName.Contains("1") || accountName.Contains("2") || accountName.Contains("3"))) | |
{ | |
_users.Add(accountName); | |
} | |
} | |
private static string _lastMessage; | |
private static void Log(string message, ConsoleColor color = ConsoleColor.Yellow, bool singleLine = false, bool currentLine = false) | |
{ | |
var temp = Console.ForegroundColor; | |
Console.ForegroundColor = color; | |
if (singleLine) | |
{ | |
if (currentLine) | |
{ | |
if (!string.IsNullOrEmpty(_lastMessage)) | |
{ | |
Console.SetCursorPosition(0, Console.CursorTop); | |
Console.Write(" ".PadLeft(_lastMessage.Length + 1)); | |
} | |
Console.SetCursorPosition(0, Console.CursorTop); | |
} | |
Console.Write(message, ConsoleColor.Green); | |
} | |
else | |
{ | |
Console.WriteLine(message, ConsoleColor.Green); | |
} | |
Console.ForegroundColor = temp; | |
_lastMessage = message; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nuget: install-package Microsoft.TeamFoundationServer.ExtendedClient