【发布时间】:2018-08-28 10:37:48
【问题描述】:
我试图加载程序集,以便可以从 powershell 连接到 TFS,但是当我尝试添加类型时出现错误。 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
它会拉取所有依赖项,因此它们存在于文件夹中,如下所示
【问题讨论】:
-
当你使用
ForEach-Object时,你不能把脚本块放在自己的行上。 -
@TheIncorrigible1 抱歉,我不得不重新运行它...摆脱了 | % in the catch....我认为 foreach-object 指的是那个....我更新了错误消息
标签: powershell tfs