小编Bad*_*Ass的帖子

从 C# 运行 powershell 脚本

我正在尝试在我的应用程序中使用 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)

c# powershell

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1

powershell ×1