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

Ask your 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.
This topic is 11 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
EBrant
Posts: 99
Last visit: Wed Apr 23, 2014 2:22 am

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

Post by EBrant »

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
Last visit: Wed Apr 23, 2014 2:22 am

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

Post by EBrant »

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
Last visit: Wed Apr 23, 2014 2:22 am

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

Post by EBrant »

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
Last visit: Wed Apr 23, 2014 2:22 am

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

Post by EBrant »

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
Last visit: Wed Apr 23, 2014 2:22 am

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

Post by EBrant »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

You cannot load 4.5 assemblies into PosH 2.0
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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.
This topic is 11 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked