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