【问题标题】:PowerShell script to get TFS collections/project list用于获取 TFS 集合/项目列表的 PowerShell 脚本
【发布时间】:2019-09-08 01:25:36
【问题描述】:

我正在使用 PowerShell 脚本从我的 TFS 服务器下载项目列表和集合。以下是我的脚本:

$uri = "http://xxxserver/tfs"
$tfsConfigurationServer = [Microsoft.TeamFoundation.Client.TfsConfigurationServerFactory]::GetConfigurationServer($uri)
$tpcService = $tfsConfigurationServer.GetService("Microsoft.TeamFoundation.Framework.Client.ITeamProjectCollectionService")

$sortedCollections = $tpcService.GetCollections() | Sort-Object -Property Name
$numberOfProjects = 0

foreach($collection in $sortedCollections) {
    $collectionUri = $uri + "/" + $collection.Name
    $tfsTeamProject = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($collectionUri)
    $cssService = $tfsTeamProject.GetService("Microsoft.TeamFoundation.Server.ICommonStructureService3")   
    $sortedProjects = $cssService.ListProjects() | Sort-Object -Property Name

    Write-Host $collection.Name "- contains" $sortedProjects.Count "project(s)

执行此脚本时,我遇到以下错误:

Unable to find type [Microsoft.TeamFoundation.Client.TfsConfigurationServerFactory]: make sure that the assembly
containing this type is loaded.
At $\getProjList.ps1:2 char:1
+ $tfsConfigurationServer = [Microsoft.TeamFoundation.Client.TfsConfigurationServe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Microsoft.TeamF...onServerFactory:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

You cannot call a method on a null-valued expression.
At $\getProjList.ps1:3 char:1
+ $tpcService = $tfsConfigurationServer.GetService("Microsoft.TeamFoundation.Frame ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At $\getProjList.ps1:5 char:1
+ $sortedCollections = $tpcService.GetCollections() | Sort-Object -Property Name
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

如何解决这个错误?

【问题讨论】:

标签: powershell tfs powershell-2.0 powershell-3.0


【解决方案1】:

您可以使用 TFS Rest API 实现目标:

$url = "http://tfs-server:8080/tfs"
$collections = Invoke-RestMethod -Uri "$url/_api/_common/GetJumpList?__v=5&navigationContextPackage={}&showStoppedCollections=false" -Method Get -UseDefaultCredentials -ContentType application/json
$collections.__wrappedArray.ForEach({
  $projects = Invoke-RestMethod -Uri "$url/$($_.name)/_apis/projects" -Method Get -UseDefaultCredentials -ContentType application/json    
  Write-Host Projects for collection $_.name
  Write-Host $projects.value.name
})

现在您拥有每个集合中的每个项目,您可以进行排序和搜索。

【讨论】:

  • 我收到此错误:方法调用失败,因为 [System.Management.Automation.PSCustomObject] 不包含名为“ForEach”的方法。在 $\getProjList.ps1:3 char:1 + $collections.__wrappedArray.ForEach({ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ + CategoryInfo : InvalidOperation: (ForEach:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
  • 检查$collections的值,我认为你没有得到结果。
  • 我看到 $Collections = @{__wrappedArray=System.Object[]}
  • 运行第一个 Invoke-RestMethod 而不放入 $collections 变量,打印的结果是什么?
  • 它显示 url = /TFS/Folder1; browser url = /tfs/_api/_browse/browse?collectionId=guid;.... 但是浏览器的url好像是错误的
【解决方案2】:

PowerShell 似乎找不到类型 Microsoft.TeamFoundation.Client.TfsConfigurationServerFactory。您可能只需要使用以下命令。它将 Microsoft .NET Framework 类型(一个类)添加到 Windows PowerShell 会话中。

您可能只使用以下命令就可以逃脱:

Add-Type -AssemblyName "Microsoft.TeamFoundation.Client"

如果这不起作用,请使用以下命令:

Add-Type -AssemblyName "Microsoft.TeamFoundation.Client.TfsConfigurationServerFactory" -UsingNamespace "Microsoft.TeamFoundation.Client"

您可能还需要运行以下命令才能使完整的脚本正常工作。

Add-Type -AssemblyName "Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory" -UsingNamespace "Microsoft.TeamFoundation.Client"

【讨论】:

    猜你喜欢
    • 2011-01-25
    • 2019-09-07
    • 2014-02-13
    • 2016-05-05
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    相关资源
    最近更新 更多