chocolatey / choco

Chocolatey - the package manager for Windows
https://chocolatey.org
Other
10.31k stars 903 forks source link

PowerShell Internal Host Fails with Mixed Mode #640

Open ferventcoder opened 8 years ago

ferventcoder commented 8 years ago

https://gist.github.com/anonymous/0791bb8be637bb20205a#file-output-log-L188-L194

VERBOSE: Loading module from path 'C:\Windows\system32\WindowsPowerShell\v1.0\Modules\BitsTransfer\BitsTransfer.psd1'.
VERBOSE: Loading 'Assembly' from path 'C:\Windows\system32\WindowsPowerShell\v1.0\Modules\BitsTransfer\Microsoft.BackgrundIntelligentTransfer.Management.Interop.dll'.
VERBOSE: Loading 'FormatsToProcess' from path 'C:\Windows\system32\WindowsPowerShell\v1.0\Modules\BitsTransfer\BitsTranfer.Format.ps1xml'.
ERROR: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

Workaround

Use the switch --use-system-powershell until this issue is resolved.

ferventcoder commented 8 years ago

Oh no - https://technet.microsoft.com/en-us/library/hh849725.aspx :

  • If you attempt to import a module that contains mixed-mode assemblies that are not compatible with Windows PowerShell 3.0, Import-Module returns an error message like the following one.

Import-Module : Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

This error occurs when a module that is designed for Windows PowerShell 2.0 contains at least one mixed-module assembly, that is, an assembly that includes both managed and non-managed code, such as C++ and C#.

To import a module that contains mixed-mode assemblies, start Windows PowerShell 2.0 by using the following command, and then try the Import-Module command again.

PowerShell.exe -Version 2.0

ferventcoder commented 8 years ago

Other resources:

ferventcoder commented 8 years ago

This is exactly the issue - http://stackoverflow.com/q/9164260/18475

mattheyan commented 8 years ago

FYI, I confirmed that the App.config fix from that link gets me past the error.

I see this is planned for 0.10.2, behind 0.10.1. What is the expected time-frame for releasing 0.10.2?

ferventcoder commented 8 years ago

@mattheyan which link was that? apologies it has been a little while since I've looked at this issue.

mattheyan commented 8 years ago

No problem. I was referring to your last comment:

This is exactly the issue - http://stackoverflow.com/q/9164260/18475

Adding a config file with the content suggested in that SO thread fixed it for me.

For default install location, create C:\ProgramData\chocolatey\choco.exe.config:

<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>
ferventcoder commented 8 years ago

Yes, that one looked like the best solution of all of the others. There are some security considerations to review to make sure that it is okay to be used. Are you experiencing this error with current packages on the community feed or internal packages?

mattheyan commented 8 years ago

Internal. In my case the package imports the "GroupPolicy" PowerShell module.

ferventcoder commented 8 years ago

Right on - perhaps create a package that places that config file as a workaround until we have something more permanent?

mattheyan commented 8 years ago

Will do. Thanks for the tip.

MichaelGerhart commented 7 years ago

Good Afternoon,

I also just encountered this issue while trying to use Invoke-SqlCmd within a package. Is there any ETA on when a fix will be issued? I am currently using the workaround above by temporarily creating choco.exe.config during runtime and deleting it at the end of the install script.

mattheyan commented 7 years ago

@michaelgg13 I created a package for it as recommended by Rob, which just creates a minimal config for choco. The down-side of course is that you have to remember to install it everywhere or add it as a dependency of packages that need it, which isn't always obvious. (This actually caused a lot of pain for us). I can provide the package if you want.

@ferventcoder If there are no immediate plans to fix the issue, would it be appropriate to publish a package for the fix as a workaround in the meantime?

ferventcoder commented 7 years ago

@mattheyan @michaelgg13 A package to fix this would be fine for now. It would also allow folks on older versions of choco to benefit once this is fixed. It's hard to determine the right way to fix this - there are some more secure things when we are in the .NET 4.x runtime that may be lost in .Net 2.x runtime (IIRC from my initial research).

aledeniz commented 5 years ago

Just to let you know I have experienced this in choco v0.10.15 on Windows 2012 R2 when running

choco install sp2013prereqs

2019-08-13 19:18:03,610 5908 [INFO ] - Downloading SharePoint 2013 prerequisite to folder:[C:\ProgramData\chocolatey\lib\SP2013PreReqs\download]
2019-08-13 19:18:03,610 5908 [INFO ] - VERBOSE: Loading module from path 'C:\windows\system32\WindowsPowerShell\v1.0\Modules\BitsTransfer\BitsTransfer.psd1'.
2019-08-13 19:18:03,626 5908 [INFO ] - VERBOSE: Loading 'Assembly' from path 'C:\windows\system32\WindowsPowerShell\v1.0\Modules\BitsTransfer\Microsoft.BackgroundIntelligentTransfer.Management.Interop.dll'.
2019-08-13 19:18:03,626 5908 [INFO ] - VERBOSE: Loading 'Assembly' from path 'C:\windows\system32\WindowsPowerShell\v1.0\Modules\BitsTransfer\Microsoft.BackgroundIntelligentTransfer.Management.Interop.dll'.
2019-08-13 19:18:03,641 5908 [INFO ] - VERBOSE: Loading 'FormatsToProcess' from path 'C:\windows\system32\WindowsPowerShell\v1.0\Modules\BitsTransfer\BitsTransfer.Format.ps1xml'.
2019-08-13 19:18:03,673 5908 [DEBUG] - Running Write-ChocolateyFailure -packageName 'SP2013PreReqsOnline' -failureMessage 'Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.' 
2019-08-13 19:18:03,688 5908 [WARN ] - WARNING: Write-ChocolateyFailure is deprecated and will be removed in v2. If you are the package maintainer, please use 'throw $_.Exception' instead.
2019-08-13 19:18:03,688 5908 [ERROR] - ERROR: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

The suggested work around did work:

choco install sp2013prereqs --verbose --log-file=C:\ProgramData\chocolatey\logs\sp2013prereqs.log --use-system-powershell

Weirdly, I didn't need the work around on Windows 2019.