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 first. The import fails, however - apparently because the module is supported only on PowerShell 3.0 (at least that's what I figure from this article). The PowerShell used by System.Management.Automation seems to be version 2.0, though.
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[] { "Hyper-V" });
Runspace runSpace = RunspaceFactory.CreateRunspace(iss);
runSpace.Open();
foreach (var err in (ArrayList)runSpace
.SessionStateProxy.PSVariable.GetValue("Error"))
Console.WriteLine(err.ToString());
runSpace.Close();
returns
The 'C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Hyper-V\Hyper-V.psd1' module cannot be imported because its manifest contains one or more members that are not valid. The valid manifest members are ('ModuleToProcess', ...). Remove the members that are not valid ('HelpInfoUri'), then try to import the module again.
Is there a way to use a specific version of PowerShell in C#?