本文介绍了使用命令行通过/登录凭据与TF.exe从Visual Studio Team Services获取最新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁有成功从Visual Studio团队服务(以前的Visual Studio Online,Team Foundation服务)版本控制服务器使用命令行获取最新的源代码,并以编程方式传递凭据?

Has anyone had success getting latest source code from the Visual Studio Team Services (formerly Visual Studio Online, Team Foundation Service) Version Control Server using the command line and passing in credentials programmatically?

- 我发现您无法使用您在命令行中登录Team Explorer或VSO网站所使用的Windows ID凭据。您需要在Team Services中为用户个人资料创建备用凭据。

-I have discovered that you can't use the Windows ID credentials that you use to login to Team Explorer or the VSO website in the command line. You need to create Alternate Credentials for the user profile in Team Services.

- 我发现如果您忽略tf.exe中的/ login,Team Services登录对话框,并要求您键入Windows ID凭据(除非它们已在您的Team Explorer或Visual Studio(或甚至可能是浏览器和Windows凭据缓存)中缓存)

-I have found out that if you omit the /login in tf.exe, the Team Services login dialog appears and asks you to type in your Windows ID credentials (unless they are already cached in your Team Explorer or Visual Studio (or even possibly Browser and Windows Credential Caches)

- 我已经发现备用凭证工作使用tf.exe的Java版本 - Team Explorer Everywhere命令行客户端(TEE CLC)。TEE CLC实际上使用您传递的/ login凭证,并允许您连接。同样的事情似乎不可能与TF.EXE在C:\Program Files(x86)\Microsoft Visual Studio 12.0 \Common7 \IDE\但是安装JAVA在这个建筑环境是对政策的。所以TEE CLC不是一个可行的选项。

-I have found out that the alternate credential work Using the Java version of tf.exe - Team Explorer Everywhere Command Line Client (TEE CLC). TEE CLC actually uses the /login credentials that you pass in and lets you connect. The same thing does NOT seem to be possible with the TF.EXE in C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ BUT INSTALLING JAVA ON THIS BUILD ENVIRONMENT IS AGAINST POLICY. So the TEE CLC is NOT a viable option.

tf get $/MyProj /collection:https://myaccount.visualstudio.com/DefaultCollection /login:user:pass 

如果您有Windows ID凭据,上述命令会忽略/ login凭证缓存或返回错误消息TF30063:您无权访问myaccount.visualstudio.com(这不是真的,因为凭据可以与Java客户端一起使用)

the above command simply ignores the /login credentials if you have the Windows ID credentials cached or it returns the error message TF30063: You are not authorized to access myaccount.visualstudio.com (which is not true, because the credentials DO work with the Java client)

推荐答案

我得到了Microsoft支持部门的答复: AA Creds for VSO目前不能与TF.EXE一起使用。 TEE CLC或使用对象模型代码是目前唯一的选择。

I got an answer from Microsoft Support: AA Creds for VSO do not work with TF.EXE at this time. TEE CLC or using object model code are the only alternatives currently. We are looking at doing this in the future.

对象模型代码指的是 Microsoft.TeamFoundation.VersionControl.Client 命名空间在dll中使用相同的名称。我最后编写了一个快速的C#控制台应用程序来下载最新的代码,而不安装Java。这种方法的另一个好处是它不需要创建一个一次性的工作区。如果你使用下面的代码创建一个名为tfsget.exe的可执行文件,它可以被调用。

Object Model Code refers to the Microsoft.TeamFoundation.VersionControl.Client Namespace in the dll by the same name. I ended up writing a quick C# console app to download the latest code without installing Java. An added benefit of this approach is that it does not require creating a throwaway Workspace.

从命令行如下:

tfsget https://myvso.visualstudio.com/DefaultCollection $/MyProj/Folder c:\Projects login password

每个文件可以使用像:

tfsget https://myvso.visualstudio.com/DefaultCollection $/MyProj/Folder c:\Projects login password silent

这里是代码,希望这有帮助直到MS更新TF.exe

here's the code, hope this helps until MS updates TF.exe

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace TfsGet
{
    class Program
    {
        static void Main(string[] args)
        {
            var tfsParams = TfsDownloadParams.Create(args);

            var tpc = new TfsTeamProjectCollection(new Uri(tfsParams.ServerUrl), tfsParams.Credentials);

            CheckAccess(tpc, tfsParams);

            Download(tpc, tfsParams);

        }

        private static void CheckAccess(TfsTeamProjectCollection tpc, TfsDownloadParams tfsParams)
        {
            try
            {
                tpc.Authenticate();
            }
            catch
            {
                Console.WriteLine("TFS Authentication Failed");
                Console.WriteLine("Server Url:{0}", tfsParams.ServerUrl);
                Console.WriteLine("Project Path:{0}", tfsParams.ServerProjectPath);
                Console.WriteLine("Target Path:{0}", tfsParams.TargetPath);
                Environment.Exit(1);
            }
        }

        static void Download(TfsTeamProjectCollection tpc, TfsDownloadParams tfsParams)
        {   
            var versionControl = tpc.GetService<VersionControlServer>();
            // Listen for the Source Control events.
            versionControl.NonFatalError += Program.OnNonFatalError;

            var files = versionControl.GetItems(tfsParams.ServerProjectPath, VersionSpec.Latest, RecursionType.Full);
            foreach (Item item in files.Items)
            {
                var localFilePath = GetLocalFilePath(tfsParams, item);

                switch (item.ItemType)
                {
                    case ItemType.Any:
                        throw new ArgumentOutOfRangeException("ItemType.Any - not sure what to do with this");
                    case ItemType.File:
                        if (!tfsParams.Silent) Console.WriteLine("Getting: '{0}'", localFilePath);
                        item.DownloadFile(localFilePath);
                        break;
                    case ItemType.Folder:
                        if (!tfsParams.Silent) Console.WriteLine("Creating Directory: {0}", localFilePath);
                        Directory.CreateDirectory(localFilePath);
                        break;
                }
            }
        }

        private static string GetLocalFilePath(TfsDownloadParams tfsParams, Item item)
        {
            var projectPath = tfsParams.ServerProjectPath;
            var pathExcludingLastFolder = projectPath.Substring(0, projectPath.LastIndexOf('/')+1);
            string relativePath = item.ServerItem.Replace(pathExcludingLastFolder, "");
            var localFilePath = Path.Combine(tfsParams.TargetPath, relativePath);
            return localFilePath;
        }

        internal static void OnNonFatalError(Object sender, ExceptionEventArgs e)
        {
            var message = e.Exception != null ? e.Exception.Message : e.Failure.Message;
            Console.Error.WriteLine("Exception: " + message);
        }
    }

    public class TfsDownloadParams
    {
        public string ServerUrl { get; set; }
        public string ServerProjectPath { get; set; }
        public string TargetPath { get; set; }
        public TfsClientCredentials Credentials { get; set; }
        public bool Silent { get; set; }

        public static TfsDownloadParams Create(IList<string> args)
        {
            if (args.Count < 5)
            {
                Console.WriteLine("Please supply 5 or 6 parameters: tfsServerUrl serverProjectPath targetPath userName password [silent]");
                Console.WriteLine("The optional 6th 'silent' parameter will suppress listing each file downloaded");
                Console.WriteLine(@"Ex: tfsget ""https://myvso.visualstudio.com/DefaultCollection"" ""$/MyProject/ProjectSubfolder"" ""c:\Projects Folder"", user, password ");

                Environment.Exit(1);
            }

            var tfsServerUrl = args[0]; //"https://myvso.visualstudio.com/DefaultCollection";
            var serverProjectPath = args[1]; // "$/MyProject/Folder Path";
            var targetPath = args[2]; // @"c:\Projects\";
            var userName = args[3]; //"login";
            var password = args[4]; //"passsword";
            var silentFlag = args.Count >= 6 && (args[5].ToLower() == "silent"); //"silent";
            var tfsCredentials = GetTfsCredentials(userName, password);

            var tfsParams = new TfsDownloadParams
            {
                ServerUrl = tfsServerUrl,
                ServerProjectPath = serverProjectPath,
                TargetPath = targetPath,
                Credentials = tfsCredentials,
                Silent = silentFlag,
            };
            return tfsParams;
        }

        private static TfsClientCredentials GetTfsCredentials(string userName, string password)
        {
            var networkCreds= new NetworkCredential(userName, password);
            var basicCreds = new BasicAuthCredential(networkCreds);
            var tfsCreds = new TfsClientCredentials(basicCreds)
            {
                AllowInteractive = false
            };
            return tfsCreds;
        }
    }
}

这篇关于使用命令行通过/登录凭据与TF.exe从Visual Studio Team Services获取最新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 17:15