PowerShell脚本获取TFS集合/项目列表

问题描述 投票:2回答:2

我正在使用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-v2.0 powershell-v3.0
2个回答
1
投票

看来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"

4
投票

您可以使用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
})

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

© www.soinside.com 2019 - 2024. All rights reserved.