【问题标题】:Add-Type: Cannot add type. The assembly 'System.IO.Compression.FileSystem' could not be foundAdd-Type:无法添加类型。找不到程序集“System.IO.Compression.FileSystem”
【发布时间】:2016-04-20 08:44:43
【问题描述】:

我在 TeamCity Builds 中使用简单的 PowerShell 脚本。

它需要System.IO.Compression.FileSystem,并且代理安装了 .NET 4.5.2。下面是安装的 .NET 框架

PSChildName         Version             Release             Product            
-----------         -------             -------             -------            
v2.0.50727          2.0.50727.5420                                             
v3.0                3.0.30729.5420                                             
Windows Communic... 3.0.4506.5420                                              
Windows Presenta... 3.0.6920.5011                                              
v3.5                3.5.30729.5420                                             
Client              4.5.51209           379893              4.5.2              
Full                4.5.51209           379893              4.5.2              
Client              4.0.0.0                                    

PowerShell 脚本有以下行

[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem");
Add-Type -AssemblyName System.IO.Compression.FileSystem

在第二行,执行失败并出现错误

Add-Type : Cannot add type. The assembly 'System.IO.Compression.FileSystem' could not be found.
At C:\BuildAgent\someFile.ps1:104 char:13
+     Add-Type <<<<  -AssemblyName System.IO.Compression.FileSystem
+ CategoryInfo          : ObjectNotFound: (System.IO.Compression.FileSystem:String) [Add-Type], Exception
+ FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands.AddTypeCommand

奇怪,但我希望使用 .NET 4.5.2,PowerShell 应该能够从 GAC 加载程序集

任何帮助将不胜感激

【问题讨论】:

  • 您使用的是哪个 PowerShell 版本?而且,更重要的是,该 PowerShell 使用什么 .NET 版本?也许你需要强制它使用 .NET 4 来成功加载这个库。
  • 我正在使用 .NET 版本 v4.0.30319 覆盖 powershell.exe.config。它显示 System.IO.Compression.FileSystem 的 GAC=true 但无法加载相同的程序集

标签: .net powershell


【解决方案1】:

尝试加载特定的 DLL:

Add-Type -Path C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.IO.Compression.FileSystem\v4.0_4.0.0.0__b77a5c561934e089\System.IO.Compression.FileSystem.dll

【讨论】:

【解决方案2】:

LoadWithPartialName() 已过时,所以尽可能避免使用它;但是,由于 LoadWithPartialName() 已经在您的上下文中工作,您还可以使用返回的对象中的 Location 属性来加载 DLL。

if($PSVersionTable.PSVersion -lt '5.0'){
   Add-Type -Path ([Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")).Location;
}else{
   Add-Type -AssemblyName System.IO.Compression.FileSystem
}

【讨论】:

  • LoadWithPartialName 已被弃用,您不应该使用它,因为您以后会遇到困难
  • @JrBriones 还有这个stackoverflow.com/a/6131116/4496560
  • @JrBriones,它是 PowerShell 的旧版本,否则使用 Add-Type -As System.IO.Compression.FileSystem
  • @JrBriones,尽可能避免更新
【解决方案3】:

我在运行 PowerShell 脚本时遇到了完全相同的错误。我猜这是安装的 .Net 版本与 PowerShell 版本的一些冲突。就我而言,帮助我将 PowerShell 版本更新为最新版本。可以在这里找到:

https://www.microsoft.com/en-us/download/details.aspx?id=40855

【讨论】:

    【解决方案4】:

    尝试添加这个(并删除最后一部分) Add-Type -AssemblyName System.IO.Compression

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 2011-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 2016-01-02
      相关资源
      最近更新 更多