9

I am using simple PowerShell script in TeamCity Builds.

It requires System.IO.Compression.FileSystem and the agent has .NET 4.5.2 installed. Below are the .NET frameworks installed

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                                    

The PowerShell script has following line

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

On the second line, the execution fails with error

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

Strange, but I expected that with .NET 4.5.2, PowerShell should be able to load assembly from GAC

Any help will be appreciated

GeekzSG
  • 943
  • 1
  • 11
  • 28
  • 1
    What PowerShell version are you using? And, more importantly, what .NET version does that PowerShell use? Maybe you'll need to force it to use .NET 4 to load this library successfully. – ForNeVeR Apr 20 '16 at 08:46
  • I am overriding powershell.exe.config with .NET version `v4.0.30319`. It shows GAC=true for `System.IO.Compression.FileSystem` but not able to load the same assembly – GeekzSG Apr 20 '16 at 09:05

4 Answers4

7

Try to load particular DLL instead:

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
Kirill Pashkov
  • 3,118
  • 1
  • 15
  • 20
  • fantastic! Can you help over here? http://stackoverflow.com/questions/39801315/how-to-chain-commands-at-a-special-powershell-4-command-prompt – johny why Sep 30 '16 at 23:12
1

LoadWithPartialName() is obsolete'd, so avoid it when you can; however since LoadWithPartialName() is already working in your context, you could also use the Location property from the object that is returned to load the 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
}
Gregor y
  • 1,762
  • 15
  • 22
0

I had the exactly same error when running PowerShell script. I guess it was some collision of installed .Net version vs. PowerShell version. In my case helped me just to update PowerShell version to the newest one. Can be found here:

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

Jan Muncinsky
  • 4,282
  • 4
  • 22
  • 40
0

Try adding this instead (and remove the last portion) Add-Type -AssemblyName System.IO.Compression

Bbb
  • 517
  • 6
  • 27