无法识别Add-windowsfeature

本文关键字:Add-windowsfeature 识别 | 更新日期: 2023-09-27 17:53:41

我试图使用c#运行powershell命令,但当我调用管道时,我一直得到错误。我想知道是否有人知道为什么我一直得到添加窗口功能不被识别。提前谢谢。

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

无法识别Add-windowsfeature

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