Call a specific version of PowerShell from C#

后端 未结 2 766
独厮守ぢ
独厮守ぢ 2021-01-27 15:03

I\'m trying to use the Get-VM Cmdlet called from C# on a Hyper-V host.

Obviously, the according PowerShell module Hyper-V has to be imported fi

2条回答
  •  梦如初夏
    2021-01-27 15:17

    A colleague figured it out:

    Apparently, .NET 4+ comes with an all new common language runtime: the CLR4
    This runtime uses its own assemblies loaded from a new assembly cache located at C:\Windows\Microsoft.NET\assembly.

    The System.Management.Automation version 3.0.0.0, which will automatically use PowerShell 3.0, exists for the CLR4 only. Because I configured my application to run under .NET 3.5, it would use the old CLR2 and could not even see the newer assembly.

    To make sure the application would still run on .NET 3.5, add this to the App.config file in the project folder:

     
    
    


    If CLR4 is available, it'll load the according GAC, find a policy file that redirects all references to System.Management.Automation version 1.0.0.0 to version 3.0.0.0 and the PowerShell-Modules work as expected.
    If you only have .NET 3.5, the older version will be loaded; PowerShell still works, but only up to version 2.0.

提交回复
热议问题