Hyper-V Cmdlets in Windows PowerShell using C#

Mar 26, 2013 at 7:14 AM
Hello ,

I have Installed /Configured Hyper-v Manager on Windows Server 2012.I want to Execute powershell command like 'GET-VM' then error coming as :-


private string RunScriptNew(string scriptText)
    {
        PowerShell ps;
        ps = PowerShell.Create();
    ps.AddScript("Set-ExecutionPolicy RemoteSigned -Force");
       //ps.Invoke();
        ps.AddScript(@"Import-Module Hyper-V");
        ps.Invoke();
        // Add the PowerShell script to be run
        ps.AddScript(scriptText);
        ps.Commands.AddCommand("Out-String");
        // execute the script
        Collection<PSObject> results = ps.Invoke();
        StringBuilder stringBuilder = new StringBuilder();
        // convert the script result into a single string
        foreach (PSObject obj in results)
        {

            stringBuilder.Append(obj.ToString());

        }

        // convert the erros into a single string

        foreach (ErrorRecord error in ps.Streams.Error)
        {
            stringBuilder.Append(error.ToString());
        }
        return (stringBuilder.ToString());

    }


"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', 'NestedModules', 'GUID', 'Author', 'CompanyName',
'Copyright', 'ModuleVersion', 'Description', 'PowerShellVersion', 'PowerShellHostName',
'PowerShellHostVersion', 'CLRVersion', 'DotNetFrameworkVersion', 'ProcessorArchitecture',
'RequiredModules', 'TypesToProcess', 'FormatsToProcess', 'ScriptsToProcess', 'PrivateData',
'RequiredAssemblies', 'ModuleList', 'FileList', 'FunctionsToExport', 'VariablesToExport',
'AliasesToExport', 'CmdletsToExport'). Remove the members that are not valid ('HelpInfoUri'),
then try to import the module again.
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."