【发布时间】:2020-09-06 00:49:22
【问题描述】:
我已经获得了团队或团队成员,但我找不到将用户添加到团队的方法。是否有我可以使用域名“域\用户”将用户添加到团队的 REST_API、命令行或 API。请指教。非常感谢
最好的问候,
【问题讨论】:
-
哪个版本? Azure DevOps 服务?你已经看过API documentation了吗?
标签: powershell command-line tfs azure-devops
我已经获得了团队或团队成员,但我找不到将用户添加到团队的方法。是否有我可以使用域名“域\用户”将用户添加到团队的 REST_API、命令行或 API。请指教。非常感谢
最好的问候,
【问题讨论】:
标签: powershell command-line tfs azure-devops
如果您使用 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,唯一的TeamFoundationId是在用户添加到Azure DevOps Server时自动生成的。我们无法使用外部工具生成 ID。
因此,要使用该 REST API,我们需要获取您想要将其添加到项目/团队的特定用户的 TeamFoundationId。
目前,在 Azure DevOps Server 2019 中没有列出 TeamFoundationId 用户的 REST API,但是我们可以通过客户端 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();
}
}
}
【讨论】: