我正在使用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
如何解决此错误?
答案 0 :(得分: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
})
现在每个集合中都有每个项目,您可以进行排序和搜索。
答案 1 :(得分: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"