Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell script to get TFS collections/project list

I am using a PowerShell script to download the list of projects and collections from my TFS server. Below is my script:

$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)

Upon executing this script I am etting the below error:

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

How can this error be resolved?

like image 936
Rick Avatar asked Jan 01 '23 01:01

Rick


2 Answers

You can achieve the goal with 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
})

Now you have each projects in each collection and you can sort and search.

like image 112
Shayki Abramczyk Avatar answered Jan 05 '23 16:01

Shayki Abramczyk


It seems that PowerShell can't find the type Microsoft.TeamFoundation.Client.TfsConfigurationServerFactory. You probably just need to use the following command. Which adds a Microsoft .NET Framework type (a class) to a Windows PowerShell session.

You might be able to get away with using just the following command:

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

If that doesn't work then use the following commands:

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

You will also probably need to run the command below for your full script to work.

Add-Type -AssemblyName "Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory" -UsingNamespace "Microsoft.TeamFoundation.Client"
like image 26
Patrick Mcvay Avatar answered Jan 05 '23 16:01

Patrick Mcvay