【问题标题】:Remotely Start Powershell Script from c# Management.Automation.Runspaces从 c# Management.Automation.Runspaces 远程启动 Powershell 脚本
【发布时间】:2015-01-28 15:54:06
【问题描述】:

我正在尝试远程执行传递对象参数的 powershell 脚本。我已经成功地做到了这一点,但现在我只想启动脚本,断开我的远程会话并让 powershell 脚本继续在远程机器上执行。不幸的是,我无法做到这一点。

到目前为止,这是我的代码,它成功运行了远程 powershell 脚本,但是如果我处理掉我的会话,powershell 脚本也会停止执行:

PowerShell ps = PowerShell.Create();
ps.Runspace = _runspace;
ps.AddCommand(scriptPath);

if (scriptParameters != null)
{
    foreach (var parameter in scriptParameters)
    {
        ps.AddParameter(parameter.Key, parameter.Value);
    }
}

ps.Invoke();

我如何才能启动 .ps1 脚本并在我断开远程会话时让它继续运行?

【问题讨论】:

    标签: c# powershell winrm


    【解决方案1】:

    您只能使用 Powershell 3+ 执行此操作。我还没有测试过,但我想连接的双方都需要支持断开连接的会话。

    在 .Net 中,您将使用 Runspace.Disconnect Method

    Powershell 等效项是 Disconnect-PSSession

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      相关资源
      最近更新 更多