无法添加类型Microsoft.TeamFoundation.Client

时间:2018-03-19 18:42:38

标签: powershell tfs

我正在尝试加载程序集,所以我可以从powershell连接到TFS但是当我尝试Add-Type时遇到错误。 nuget包已成功下载,并且该文件存在我想要添加的程序集。

我的代码如下

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

出现以下错误

        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]  

此外,当它出错时,某些东西仍然锁定我试图加载的DLL,阻止我在下次运行脚本时清除目录。 (我必须关闭visual studio并重新打开它才能删除文件锁)

编辑:只需澄清一下,当我NuGet下面的包

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

它会使用它来提取所有依赖项,因此它们存在于文件夹中,如下所示

enter image description here

3 个答案:

答案 0 :(得分:0)

显然您收到此错误是因为缺少依赖项,因为您的例外显示...

无法加载文件或程序集Microsoft.VisualStudio.Services.Common

来自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)

答案 1 :(得分:0)

您还需要添加Microsoft.VisualStudio.Services.Common.dll

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

Add-Type -Path $assemblyPath1

答案 2 :(得分:0)

使用电源外壳时出现相同的错误

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

,解决方法是: 用Add-Type -Path替换LoadWithPartialName并工作

#[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"

调整dll的路径。