0

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();
}

1 Answer 1

1

Resolved,changed from 3.5 to .net 4.5 and do the trick,(any .net 4+ should work)seems that Hyperv module is not supported on that version,or something like that and was causing the trouble and not loading the modules.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.