将成员添加到Azure DevOps团队

时间:2020-05-19 20:03:50

标签: powershell command-line tfs azure-devops

我已经可以招募团队或团队成员,但是我无法找到将用户添加到团队中的方法。是否有RESt_API,命令行或API,可以使用域名“ domain \ user”将用户添加到团队中。请指教。非常感谢

最好的问候

1 个答案:

答案 0 :(得分:0)

如果使用Azure DevOps服务(https://dev.azure.com/xxxx),则可以使用Members - Add REST API。

如果使用Azure DevOps Server,则未记录用于将成员添加到项目和团队的REST API。解决方法是,我们可以通过在浏览器中按F12并选择Network来跟踪此rest api。

示例:

POST http://TFS2019:8080/tfs/{Collection}/{project}/_api/_identity/AddIdentities?api-version=5.0

Request Body:

        {
            "newUsersJson": "[]",
            "existingUsersJson": "[\"55b98726-c6f5-48d2-976b-xxxxxx\"]",
            "groupsToJoinJson": "[\"7283653f-54b2-4ebf-86c3-xxxxxxx\"]",
            "aadGroupsJson": "[]"
        }

但是,正如我们所看到的,我们只能在请求json主体中使用用户和团队/组GUID。对于特定的团队,我们可以使用REST APIs Projects和团队来获取其GUID。 对于用户而言,实际上是使用TeamFoundationId,将用户添加到Azure DevOps Server时会自动生成唯一的TeamFoundationId。我们无法使用外部工具生成ID。

因此,要使用该REST API,我们需要获取要添加到项目/团队中的特定用户的TeamFoundationId

当前,Azure DevOps Server 2019中没有REST API列出TeamFoundationId个用户,但是我们可以使用客户端API来获得它:

以下示例供您参考以获取特定用户的TeamFoundationId:(它还将用户列表及其TeamFoundationId导出到userlist.txt

using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using System.Linq;
using System.IO;

namespace Getuserlist

{

    class Program

    {
        static void Main(string[] args)

        {

            TfsConfigurationServer tcs = new TfsConfigurationServer(new Uri("https://wsicads2019"));

            IIdentityManagementService ims = tcs.GetService<IIdentityManagementService>();

            TeamFoundationIdentity tfi = ims.ReadIdentity(IdentitySearchFactor.AccountName, "[DefaultCollection]\\Project Collection Valid Users", MembershipQuery.Expanded, ReadIdentityOptions.None);

            TeamFoundationIdentity[] ids = ims.ReadIdentities(tfi.Members, MembershipQuery.None, ReadIdentityOptions.None);

            using (StreamWriter file = new StreamWriter("userlist.txt"))

                foreach (TeamFoundationIdentity id in ids)

                {
                    if (id.Descriptor.IdentityType == "System.Security.Principal.WindowsIdentity" && id.UniqueName == "Domain\\User")

                    { Console.WriteLine("[{0},{1}]", id.UniqueName, id.TeamFoundationId); }

                    file.WriteLine("[{0},{1}]", id.UniqueName, id.TeamFoundationId);
                }

            var count = ids.Count(x => ids.Contains(x));
            Console.WriteLine(count);
            Console.ReadLine();
        }
    }
}