我正在尝试在我的应用程序中使用 c# 编写一个 powershell 脚本。问题是,如果我用 c# 运行脚本,它就不能工作,但如果我手动运行它,它就可以工作。
那是脚本: DISM /online /Enable-Feature /Featurename:NetFx3 /All
该脚本在 Windows 服务器上启用框架 3.5。来源
我的代码:
纽格: System.Management.Automation
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("DISM /online /Enable-Feature /Featurename:NetFx3 /All ");
// begin invoke execution on the pipeline
IAsyncResult result = PowerShellInstance.BeginInvoke();
// do something else until execution has completed.
// this could be sleep/wait, or perhaps some other work
while (result.IsCompleted == false)
{
Console.WriteLine("Waiting for pipeline to finish...");
Thread.Sleep(1000);
// might want to place a timeout …Run Code Online (Sandbox Code Playgroud)