【问题标题】:Using C# to Call a PowerShell Script to Execute the Windows 'djoin' Command使用 C# 调用 PowerShell 脚本以执行 Windows 'djoin' 命令
【发布时间】:2015-03-25 07:49:50
【问题描述】:
  1. 我有以下 Windows 批处理命令,可从命令提示符成功运行:djoin /provision /domain /machineou /machine /savefile

  2. 我已经能够将这个 Windows 命令包装在一个 PowerShell 命令中:
    Invoke-Expression [djoin command]
    并且在使用 PowerShell 在本地运行它时效果很好.

  3. 当我尝试在第 2 步中获取脚本并从 C# Web 应用程序调用它时失败了。我正在尝试以下方法:

    PowerShell ps = PowerShell.Create();
    ps.addCommand("Invoke-Expression");
    ps.AddArgument("<djoin command>");
    

网页没有给我任何错误,我被困在这个问题上。如果您有任何问题,请告诉我,感谢您的帮助。

【问题讨论】:

  • 你熟悉Process and ProcessInfo Classes 和ShellExecute cmd 的...吗?

标签: c# powershell


【解决方案1】:

如果你真的想使用 PowerShell 引擎来执行这个,那么就这样做吧:

using (var ps = PowerShell.Create()) 
{
    ps.AddScript("djoin /provision /domain /machineou /machine /savefile");
    var results = ps.Invoke();
    foreach (var r in results) 
    {
        // do something with r
    }
}

注意:不需要使用 Invoke-Expression。此外,正如 MethodMan 在他的评论中建议的那样,您可以使用 System.Diagnostics.Process.Start()

【讨论】:

    猜你喜欢
    • 2010-10-06
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 2017-06-08
    相关资源
    最近更新 更多