【问题标题】:How to keep remote powershell command alive after session end?会话结束后如何保持远程powershell命令有效?
【发布时间】:2013-07-17 16:45:44
【问题描述】:

我使用以下命令在远程 Windows 机器上运行 setup_server.exe

powershell -command "$encpass=convertto-securestring -asplaintext RPASSWORD -force;$cred = New-Object System.Management.Automation.PSCredential -ArgumentList RUSER,$encpass; invoke-command -computername RCOMPUTERNAME -scriptblock {setup_server.exe} -credential $cred;"

setup_server.exe 的任务是创建一些配置文件并启动my_server.exe(一些守护进程),然后它就完成了。我希望my_server.exesetup_server.exe 完成后继续运行。

因此,当我通过本地机器上的 CMD 执行此操作时(即仅从 CMD 运行 setup_server.exe)它可以工作,但是当我通过远程主机上的 powershell 执行此操作时它不起作用。即my_server.exe 启动,但在setup_server.exe 关闭后服务器也关闭(杀死)。

所以问题如下: 我应该使用哪些 powershell 标志/cmdlet 来使所描述的场景在本地模式下工作?

注意:我想同步获取setup_server.exe 的输出,因此使用-AsJob 标志运行远程命令可能对我不起作用,尽管我什至不知道它是否会在@987654332 之后保持服务器活动@的结束。

【问题讨论】:

  • 要么创建服务或计划任务来运行您的 exe 吗?
  • 如果你通过Start-Process(非阻塞的ps会话)而不是直接启动setup_server.exe是否有效?

标签: windows powershell cmd powershell-2.0 remote-access


【解决方案1】:

在命令完成后保持远程 PowerShell 会话运行的方法是使用 PSSession 例如:

$s = new-PSSession computername
Invoke-Command -session $s { ..script.. }
... do other stuff, remote powershell.exe continues to run
Remove-PSSession $s # when you're done with the remote session

尽管 exe 应该独立于启动它们的应用程序运行。

【讨论】:

  • 感谢您抽出宝贵时间回答@Keith Hill,但它不起作用。我试过这个:powershell -command \"\$encpass=convertto-securestring -asplaintext mypassword -force;\$cred = New-Object System.Management.Automation.PSCredential -ArgumentList myuser,\$encpass;\$session = new-PSSession -computername HOSTNAME -credential \$cred; invoke-command -session \$session -scriptblock {setup_server.exe};\" 但在 setup_server 完成后服务器再次被杀死。
【解决方案2】:

您为什么使用 Invoke-Command。如果您想要一个持久会话,请使用 Enter-PSSession。

$s = New-PSSession -Computername "Computername";
Enter-PSSession -Session $s;

setup_server.exe

# Once you are finnished
Exit-PSSession

使用“Enter-PSSession”,您不仅可以在服务器上调用某些命令,还可以直接登录,就像您可能知道的 SSH 一样。

【讨论】:

  • # Once you are finnished 这不会发生。我想启动远程服务器,仅此而已。然后我想退出会话,保持服务器在远程主机上运行。
  • 如果你不关闭shell,我认为会话应该保持打开状态。至少有一段时间......使用'New-PSSessionOption -IdleTimeOut ??? -操作超时??? -取消超时??? -OpenTimeOut ???'您可以设置超时:technet.microsoft.com/en-us/library/hh849703.aspx
  • 我是在脚本中做的,所以 shell 被关闭了。
【解决方案3】:

如果您希望您的 powershell 会话继续运行,因为您正在运行一个 exe,请尝试使用 -InDisconnectedSession 开关。据我了解,它将在实际上未连接到您的计算机的会话中在远程计算机上运行可执行文件。本质上,您的计算机在断开连接时不会破坏会话,从而允许 exe 继续运行。

invoke-command -computername RCOMPUTERNAME -scriptblock {start-process setup_server.exe} -InDisconnectedSession

如果您需要在多台计算机上执行此操作。设置所有计算机名称的数组。

注意:我认为这不适用于已创建的会话。

【讨论】:

    【解决方案4】:

    为了让 powershell 代码在会话退出时运行,它应该是一个进程。而windows保持进程的方式是运行一个.exe或者windows服务。

    【讨论】:

      【解决方案5】:

      为了在执行命令后保持 Powershell shell 打开,我使用了 -NoExit 开关,例如此脚本使用用户管理员在 servername 上启动远程交互式 PS 会话

      C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit 
          -Command "Enter-PSSession -ComputerName servername -Credential administrator"
      

      http://powershell-guru.com/powershell-tip-13-prevent-powershell-from-exiting-once-script-finished/

      【讨论】:

        猜你喜欢
        • 2018-07-14
        • 2012-01-16
        • 2020-07-04
        • 2016-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-01
        • 2012-10-25
        相关资源
        最近更新 更多