0

我正在尝试远程执行传递对象参数的 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 脚本并让它继续运行?

4

1 回答 1

0

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

在 .Net 中,您将使用Runspace.DisconnectMethod

Powershell 等价物是Disconnect-PSSession.

于 2015-01-29T03:26:41.163 回答