我正在尝试使用C#运行powershell命令,但是在调用流水线时一直出现错误。我想知道是否有人知道为什么会出现add-windowsfeature is not recognized的问题。提前感谢。
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
之前,你可以尝试执行import-module
命令并查看其结果如何。 - Despertar