【问题标题】:Run several "long" scripts with remote Powershell使用远程 Powershell 运行多个“长”脚本
【发布时间】:2018-01-12 03:21:32
【问题描述】:

我有 3 个 Powershell 脚本:A.ps1B.ps1RunAB.ps1

A.ps1B.ps1 在同一台机器上,RunAB.ps1 在不同的机器上。

我想让RunAB.ps1 运行。通过远程powershell,A和B同时(即并行)。

A 和 B 都需要大约 2-3 天才能完成运行。

为了做到这一点,我写了RunAB.ps1(仅相关行):

Invoke-Command -ComputerName $physical_server_ip -ScriptBlock {Set-Location "C:\Test"; Powershell.exe -File "A.ps1"} -Credential $physical_credential -AsJob

Invoke-Command -ComputerName $physical_server_ip -ScriptBlock {Set-Location "C:\Test"; Powershell.exe -File "B.ps1"} -Credential $physical_credential -AsJob

这在开始时运行良好,但大约 2 小时后,远程机器上的 A 和 B 的 powershell 进程都被终止(它们没有完成)......

如果我从第二个Invoke-Command 中删除AsJob(导致RunAB.ps1 等待此命令的原因),那么它似乎可以解决问题 - A 和 B 继续在远程计算机上运行,​​但我没有想让RunAB.ps1等待..

做我想做的事的正确方法是什么?

谢谢

【问题讨论】:

    标签: powershell powershell-remoting


    【解决方案1】:

    您需要延长您要连接的 PSSession 的空闲超时。默认为7200000,即 2 小时

    查看当前超时值:

    Get-PSSessionConfiguration | Select-Object Name, *Timeout*
    

    设置超时值:

    Invoke-Command -ComputerName $physical_server_ip -SessionOption {New-PSSessionOption -IdleTimeout XXXXXXX} ...
    #Where XXXXXX is the value in milliseconds less than the Max timeout.
    

    【讨论】:

    • 感谢您的回答,没有New-SessionOption,您的意思是New-PsSessionOption 吗?
    • @Noam 我做了,修正了我回答中的错字。
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 2022-10-12
    • 1970-01-01
    • 2019-12-03
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    相关资源
    最近更新 更多