2

I was trying to load the assembly so I could connect to TFS from powershell but I'm getting an error when I try to Add-Type. The nuget package was successfully downloaded and the file exists for the assembly I want to add.

My code is as follows

$sourceCodeDirectory = "C:\testing123";

CleanDirectory -directory $sourceCodeDirectory

[System.IO.Directory]::SetCurrentDirectory($sourceCodeDirectory);
$cwd = [System.IO.Directory]::GetCurrentDirectory();

Write-Output("CurrentWorkingDirectory: $cwd");

$sourceNugetExe = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
$targetNugetExe = "$cwd\nuget.exe"
Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe
Set-Alias nuget $targetNugetExe -Scope Global -Verbose
nuget install Microsoft.TeamFoundationServer.Client -version '15.112.1' -OutputDirectory $cwd
nuget install Microsoft.TeamFoundationServer.ExtendedClient -version '15.112.1' -OutputDirectory $cwd
$uri = New-Object System.Uri -ArgumentList $TFSCollectionUri

try
{
    Write-Output 'Loading TFS Assemblies...'

    $assemblyPath = $cwd + "\Microsoft.TeamFoundationServer.ExtendedClient.15.112.1\lib\net45\Microsoft.TeamFoundation.Client.dll";
    Write-Output($assemblyPath);

    Add-Type -Path $assemblyPath
}
catch
{
    $_.LoaderExceptions 
    {
        Write-Error $_.Message
    }
}

With the following error

        Write-Error $_.Message

[ERROR] new-object : Could not load file or assembly 
[ERROR] 'Microsoft.VisualStudio.Services.Common, Version=15.0.0.0, Culture=neutral, 
[ERROR] PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot 
[ERROR] find the file specified.
[ERROR] At C:\Users\user\Documents\Visual Studio 2015\Projects\Build_FormsDesigne
[ERROR] r\Build_FormsDesigner\Build_FormsDesigner.ps1:107 char:23
[ERROR] + ... lProvider = new-object Microsoft.TeamFoundation.Client.UICredentialsP ...
[ERROR] +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ERROR]     + CategoryInfo          : NotSpecified: (:) [New-Object], FileNotFoundExce 
[ERROR]    ption
[ERROR]     + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerS 
[ERROR]    hell.Commands.NewObjectCommand
[ERROR]  
[ERROR] Exception calling "GetTeamProjectCollection" with "2" argument(s): "Could not 
[ERROR] load file or assembly 'Microsoft.VisualStudio.Services.Common, 
[ERROR] Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of 
[ERROR] its dependencies. The system cannot find the file specified."
[ERROR] At C:\Users\user\Documents\Visual Studio 2015\Projects\Build_FormsDesigne
[ERROR] r\Build_FormsDesigner\Build_FormsDesigner.ps1:108 char:1
[ERROR] + $collection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollecti ...
[ERROR] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ERROR]     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
[ERROR]     + FullyQualifiedErrorId : FileNotFoundException
[ERROR]  
[ERROR] Method invocation failed because [System.String] does not contain a method 
[ERROR] named 'Authenticate'.
[ERROR] At C:\Users\user\Documents\Visual Studio 2015\Projects\Build_FormsDesigne
[ERROR] r\Build_FormsDesigner\Build_FormsDesigner.ps1:109 char:1
[ERROR] + $collection.Authenticate()
[ERROR] + ~~~~~~~~~~~~~~~~~~~~~~~~~~
[ERROR]     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
[ERROR]     + FullyQualifiedErrorId : MethodNotFound
[ERROR]  

Also when it errors out, something still has a lock on the DLL I was trying to load, preventing me from clearing the directory the next time I run the script. ( I have to close visual studio and re open it in order to remove the file lock)

EDIT: Just clarify, when I NuGet the following package

nuget install Microsoft.TeamFoundationServer.Client -version '15.112.1' -OutputDirectory $cwd

It pulls all the dependencies with it, so they exist in the folder as you can see below

enter image description here

erotavlas
  • 4,274
  • 4
  • 45
  • 104

3 Answers3

1

Apparently you are getting this error because there are missing dependencies, as your exception is showing ...

Could not load file or assembly Microsoft.VisualStudio.Services.Common

From https://www.nuget.org/packages/Microsoft.TeamFoundationServer.Client/

Dependencies  
.NETFramework 4.5 
Microsoft.AspNet.WebApi.Client (>= 5.2.2)  
Microsoft.TeamFoundation.DistributedTask.Common (= 15.112.1)  
Microsoft.VisualStudio.Services.Client (= 15.112.1)  
Newtonsoft.Json (>= 8.0.3)
Rodrigo Werlang
  • 2,118
  • 1
  • 17
  • 28
  • Does it work when you add Microsoft.VisualStudio.Services.Common.dll to Add-Type? – Rodrigo Werlang Mar 20 '18 at 13:47
  • Add all the dependencies. Use the regular one. What you want to do with the client? Wouldn't the rest api be a choice? maybe – Rodrigo Werlang Mar 20 '18 at 14:32
  • I just want to do like this (connect to TFS, create local workspace, get a label, close the connection that's pretty much it) unfortunately I cannot find any updated examples that do not rely on the GAC for adding the types. https://alistairbmackay.wordpress.com/2016/02/01/manipulating-tfs-with-powershell-part-1-connecting/ OR this http://www.softwarepronto.com/2013/11/build-automation-how-to-correctly-pull.html – erotavlas Mar 20 '18 at 14:33
  • You could use tf.exe command line. tf get /v:LabelName - ref https://learn.microsoft.com/en-us/vsts/tfvc/get-command – Rodrigo Werlang Mar 20 '18 at 15:13
0

You need to add Microsoft.VisualStudio.Services.Common.dll too:

$assemblyPath1 = $cwd + "\Microsoft.VisualStudio.Services.Client.15.112.1\lib\net45\Microsoft.VisualStudio.Services.Common.dll";    

Add-Type -Path $assemblyPath1
Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • Add-Type locks the DLL so I am unable to clean the directory when finished. Do you know how to release the lock on it, when the script completes? – erotavlas Mar 20 '18 at 13:42
  • 1
    Please check the solution in this case by using a nested shell: https://stackoverflow.com/questions/3369662/can-you-remove-an-add-ed-type-in-powershell-again, hope this helps you. – Cece Dong - MSFT Mar 23 '18 at 09:37
0

I had the same error when using power shell

Exception calling "GetConfigurationServer" with "1" argument(s): "Could not load file or assembly 'Microsoft.TeamFoundation.Common

and the fix is: replace the LoadWithPartialName with Add-Type -Path and worked

#[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client") | Out-Null
Add-Type -Path "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.Common.dll"

Adjust the path to the dlls.

user3634685
  • 431
  • 3
  • 7
  • 15