首页 > 解决方案 > 使用 Powershell 和 Visual Studio 2019 查询 TFS 2015 源代码存储库

问题描述

我正在尝试使用 Visual Studio 2019 创建一个 Powershell 脚本,以访问特定的 Team Foundation Server 2015 团队项目及其分支之一。目标是返回在特定时间段内创建或修改的文件的名称。为此,我的印象是我必须过滤 .CreationTime 和 .LastWriteTime 属性。

我已使用 [Microsoft.TeamFoundation.Client] API 成功访问项目集合,但在使用版本控制存储库实例化 [Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer] 时无法访问单个团队项目。已确保身份验证,但没有 vcs 方法识别存储库路径。(我故意从下面的代码中省略了程序集声明。)

    $tfsCollectionURI =[System.Uri]'http://tfs.infosys.com:8080/tfs/collection/'
    $projectsCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsCollectionURI)

   if($projectsCollection.HasAuthenticated)
  {
       $vcs = $projectsCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
      }

            $projectPath = 'http://tfs.infosys.com:8080/tfs/collection/
            project/_git/Enterprise'
    $vcs.GetAllTeamProjects($true)
    $vcs.GetTeamProject('EHBs')
    $vcs.GetItems($projectPath)

标签: powershelltfs-2015visual-studio-2019

解决方案


根据您的共享代码,您正在尝试获取错误的 projectPath。

http://tfs.infosys.com:8080/tfs/collection/project/_git/Enterprise这似乎是 git repo 门户网站中的一个 url。

但是,您的代码用于TFVC 版本控制。项目路径应该是$/MyFirstProject/Main-branch2.0-child1

您可以使用下面的这个 rest api在团队项目中找到 Git Repos:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}?api-version=4.1

您可以使用 GET Invoke-RestMethod 在 Powershell 中执行 REST API,对于 PowerShell REST API 编程,请参考以下链接:

https://wilsonmar.github.io/powershell-rest-api/


推荐阅读