I have problem trying to use the Get-VM command of the Hyper-V to get the status of the vms,I trying to use powershell via c#.
I search multiple topics,have alredy executionpolicy to unrestricted in 32 and 64 bits powershell, and the hyperv module is in both folders of the powershell(in the /modules folder in syswow32 powershell and in the normal) im runing windows 8.1. In both powershell the commands works witouht need to import nothing, so I'm really in a loss.
The response when I execute is: Error in script : The term 'Get-VM' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
private void RunScript(string scriptText)
{
// create Powershell runspace
InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new[] { "C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\Modules\\Hyper-V\\Hyper-V.psd1", "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\Modules\\Hyper-V\\Hyper-V.psd1" });
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("Get-VM");
// add an extra command to transform the script
// output objects into nicely formatted strings
// remove this line to get the actual objects
// that the script returns. For example, the script
// "Get-Process" returns a collection
// of System.Diagnostics.Process instances.
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
textBoxOutput.Text+= stringBuilder.ToString();
runspace.Close();
}