无法识别add-windowsfeature

时间:2013-01-17 00:27:28

标签: c# windows powershell exception-handling

我正在尝试使用C#运行powershell命令,但在调用管道时我一直遇到错误。我想知道是否有人知道为什么我一直得到add-windowsfeature无法识别。提前谢谢。

private static void RunScript(string name)
{
    InitialSessionState initial = InitialSessionState.CreateDefault();
    initial.ImportPSModule(new[] { "ServerManager"});
    Runspace runspace = RunspaceFactory.CreateRunspace(initial); 
    // create Powershell runspace

    runspace.Open();

    RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace);
    runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");


    Pipeline pipeline = runspace.CreatePipeline();
    Command cm = new Command("Import-module");
    cm.Parameters.Add("name","ServerManager");
    pipeline.Commands.Add(cm);
    Command command = new Command("add-windowsfeature"); 
    command.Parameters.Add(null, name);  
    pipeline.Commands.Add(command);

    var a = pipeline.Invoke();
    foreach (var psObject in a)
    {
        Console.WriteLine(psObject);
    }

    runspace.Close();
}

1 个答案:

答案 0 :(得分:1)

ServerManager是一个仅64位的模块(C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules下不存在,但存在于C:\Windows\System32\WindowsPowerShell\v1.0\Modules下)。编译为x64,您的代码应该可以工作。

相关问题