3

I'm trying to write a script to connect to TFS using powershell, however I'm stuck on the part of actually connecting

$credentialProvider = new-object Microsoft.TeamFoundation.Client.UICredentialsProvider
    $collection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($uri, $credentialProvider)

It gives an error that says it cannot find the type

[ERROR] New-object : Cannot find type [ERROR] [Microsoft.TeamFoundation.Client.UICredentialsProvider]: verify that the [ERROR] assembly containing this type is loaded.

Well I tried to do this first, but it did not help

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.WorkItemTracking.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")

I only have Visual Studio 2015 installed on my development environment. Is there some component that I'm missing that is a requirement for interfacing with TFS using powershell?

Furthermore I don't know where this script will be run from (it wont be from a development machine), presumably from a machine that has access to TFS server directly maybe using Team Explorer.

erotavlas
  • 4,274
  • 4
  • 45
  • 104

3 Answers3

4

The Team Foundation Server Client Object Model used to be installed to the Global Assembly Cache when you installed Team Explorer 2013 or below. Because of that, they were always easy to load from any script.

With Team Explorer and Visual Studio 2015 and up, the packages are no longer registered globally. At the same time, Microsoft changed the license and made these assemblies distributable with your application and released a NuGet package to make distribution easier.

The proper way to handle scenarios where you need the TFS Client Object Model is to package them with your script or the download them on-demand using Nuget.

There are a number of packages that you may or may not need depending on what you are doing from your scripts:

Traditional Client Object Model:

New-style REST API object model:

You can use this little snippet to fetch nuget.exe and the dependencies on the fly: https://stackoverflow.com/a/26421187/736079 or use the install-package that was introduced in powershell v5.

Note: If you're updating or creating new scripts, it's recommended to switch to the new-style REST API's and the object model that goes along with that.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • Thanks, I managed to search my entire hard disk, and I did find the dlls eventually. As they were not part of the GAC even after installing Team Explorer 2017. So is packaging them up with the PowerShell script a possibility? (i'm working with a PowerSHell Script project in Visual Studio using the extension PowerShell tools for VS 2015) – erotavlas Mar 19 '18 at 16:33
  • Actually nevermind, I just checked and there is no option to add nuget packages to this project type. Any other options? – erotavlas Mar 19 '18 at 16:35
  • is this what you mean? https://stackoverflow.com/questions/16657778/install-nuget-via-powershell-script – erotavlas Mar 19 '18 at 16:42
  • 1
    @erotavlas Alternatively... You can use the `Install-Package` cmdlets from PSv5 to point at nuget – Maximilian Burszley Mar 19 '18 at 17:26
  • do you know of any examples or documentation how to use the rest api with powershell? – erotavlas Mar 20 '18 at 14:47
  • https://learn.microsoft.com/en-us/vsts/integrate/get-started/client-libraries/samples – jessehouwing Mar 20 '18 at 17:47
1

Here is what I used to pull in the dll's for 2013,2015 tfs

function Connect-ToTfs
{
    Param([string] $Collectionurl)
    #the collection url will be cast as a uri to the getteamproject collection. 
    Write-Verbose $Collectionurl
    if ($CollectionUrl -ne '')
    {
        #if collection is passed then use it and select all projects
        $tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection([uri]$CollectionUrl)
    }
    else
    {
        #if no collection specified, open project picker to select it via gui
        $picker = New-Object Microsoft.TeamFoundation.Client.TeamProjectPicker([Microsoft.TeamFoundation.Client.TeamProjectPickerMode]::NoProject, $false)
        $dialogResult = $picker.ShowDialog()
        if ($dialogResult -ne 'OK')
        {
            #exit
        }
        $tfs = $picker.SelectedTeamProjectCollection
    }
    $tfs    
}
function Invoke-VisualStudioDlls
{
    if (Test-Path 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer')
    {
        Write-Verbose "importing Visual Studio 2015 Dll's"
        Invoke-Visual15StudioDlls
    }
    elseif (Test-Path 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0')
    {
        Write-Verbose "importing Visual Studio 2013 Dll's"
        Invoke-Visual13StudioDlls
    }
}
function Invoke-Visual15StudioDlls
{
    $visualStudiopath = 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer'
    #$visualStudiopath45 = 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer'
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.VersionControl.Client.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Common.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Client.dll"
    Add-type -path "$visualStudiopath\Microsoft.TeamFoundation.ProjectManagement.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Build.Common.dll"

}

function Invoke-Visual13StudioDlls
{
    $visualStudiopath = 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0'
    $visualStudiopath45 = 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\ide\ReferenceAssemblies\v4.5'
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.VersionControl.Client.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Common.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Client.dll"
    Add-type -path "$visualStudiopath45\Microsoft.TeamFoundation.ProjectManagement.dll"  
}
Thom Schumacher
  • 1,469
  • 13
  • 24
0

In Visual Studio 2015, the object model client libraries are removed from GAC. In order to load them you need to point the Add-Type cmdlet to a path, for example:

Add-Type -Path "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.Client.dll"

Otherwise, you could install package from Nuget as @Jessehouwing mentioned.

Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39