Cannot load type from command line or script or ISE but can from PowerGUI script editor

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
EBrant
Posts: 99
Joined: Thu Feb 02, 2012 1:51 am

Cannot load type from command line or script or ISE but can from PowerGUI script editor

Post by EBrant » Mon Feb 18, 2013 10:02 pm

Hello,

Can someone please help me with the following question.

I have .NET 4.5 installed

From PowerGUI if I do

[System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
[IO.Compression.ZipFile]

Works as expected (i.e. can create zip files)

However if I do the above from the command line, script or PowerShell ISE I receive the following error

Unable to find type [IO.Compression.ZipFile]: make sure that the assembly containing this type is loaded.
At line:2 char:25
+ [IO.Compression.ZipFile] <<<<
+ CategoryInfo : InvalidOperation: (IO.Compression.ZipFile:String) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound

I assume PowerGUI is loading (or autoloading) I am on PS v2 some thing the other are not,

Thanks in advance
Ernie

User avatar
EBrant
Posts: 99
Joined: Thu Feb 02, 2012 1:51 am

Cannot load type from command line or script or ISE but can from PowerGUI script editor

Post by EBrant » Mon Feb 18, 2013 10:58 pm

Interestingly when I do the following from the PowerGUI editor

cls
Add-Type -path C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.IO.Compression.FileSystemv4.0_4.0.0.0__b77a5c561934e089System.IO.Compression.FileSystem.dll

I receive no errors,

when I do the above from the Powershell prompt or ISE I receive

Add-Type : Could not load file or assembly 'file:///C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.IO.Compression.FileSystemv4.0_4.0.0.0__b77a5c561934e089System.IO.Compression.FileSystem.dll' or one of its dependencies. This assem
bly is built by a runtime newer than the currently loaded runtime and cannot be loaded.
At line:2 char:9
+ Add-Type <<<< -path C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.IO.Compression.FileSystemv4.0_4.0.0.0__b77a5c561934e089System.IO.Compression.FileSystem.dll
+ CategoryInfo : NotSpecified: (:) [Add-Type], BadImageFormatException
+ FullyQualifiedErrorId : System.BadImageFormatException,Microsoft.PowerShell.Commands.AddTypeCommand

Do I need to reinstall PowerShell v2 following the addition of .net 4.5?

Thanks all
Ernie

User avatar
EBrant
Posts: 99
Joined: Thu Feb 02, 2012 1:51 am

Cannot load type from command line or script or ISE but can from PowerGUI script editor

Post by EBrant » Tue Feb 19, 2013 12:07 am

I confirmed it is definately note loading i.e.

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
[appdomain]::currentdomain.getassemblies() | select fullname

Shows the new Assembly loaded as one would expect, where as

[System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem)

[appdomain]::currentdomain.getassemblies() | select fullname

Does not show any more assembiles loaded.

But I do now know why, at the end of the say I need to invoke my script from the command line.

As mentioned above if I run my script from within the PowerGUI script editior it runs, i.e. load the Assembly, so I am thinking PowerGUI when started loads an Assembly (or what ever) that helps the above to load.

I would be most grateful if someone could help me out on this odd issue.

Thanks
Ernie

User avatar
EBrant
Posts: 99
Joined: Thu Feb 02, 2012 1:51 am

Cannot load type from command line or script or ISE but can from PowerGUI script editor

Post by EBrant » Tue Feb 19, 2013 12:08 am

The answer has got to be here

[reflection.assembly]::LoadFile('C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.IO.Compression.FileSystemv4.0_4.0.0.0__b77a5c561934e089System.IO.Compression.FileSystem.dll')

Exception calling "LoadFile" with "1" argument(s): "This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded. (Exception from HRESULT: 0x8013101B)
At C:ernieGet-Assemblies.ps1:3 char:32
+ [reflection.assembly]::LoadFile <<<< ('C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.IO.Compression.FileSystemv4.0_4.0.0.0__b77a5c561934e089System.IO.Compression.FileSystem.dll')
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

I am using PS v2, shorly I do need to upgrade to v3, after all it worked when run from within PowerGUI script editor, but that is not good to me as I need to execute the script from the command line

Thanks all in advance
Ernie

User avatar
EBrant
Posts: 99
Joined: Thu Feb 02, 2012 1:51 am

Cannot load type from command line or script or ISE but can from PowerGUI script editor

Post by EBrant » Tue Feb 19, 2013 1:45 am

Looks like one of my heros Mr Thomas Lee has the answer here

http://tfl09.blogspot.co.uk/2010/08/usi ... -with.html

Thanks All

Ernie

User avatar
jvierra
Posts: 13792
Joined: Tue May 22, 2007 9:57 am
Contact:

Cannot load type from command line or script or ISE but can from PowerGUI script editor

Post by jvierra » Tue Feb 19, 2013 4:16 pm

You cannot load 4.5 assemblies into PosH 2.0

User avatar
jvierra
Posts: 13792
Joined: Tue May 22, 2007 9:57 am
Contact:

Cannot load type from command line or script or ISE but can from PowerGUI script editor

Post by jvierra » Wed Feb 20, 2013 4:13 am

Sorry but the answer si still that you cannot use this with 2.0 and you cannot use it without installing Net 4.5 which will not install on XP or WS2003.

Locked