7

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

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.exe后继续运行setup_server.exe

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

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

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

4

5 回答 5

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 知道的那样。

于 2013-07-19T10:35:52.580 回答
2

在命令完成后保持远程 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 应该独立于启动它们的应用程序运行。

于 2013-07-19T01:07:21.363 回答
1

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

调用命令 -computername RCOMPUTERNAME -scriptblock {start-process setup_server.exe} -InDisconnectedSession

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

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

于 2013-07-29T21:26:31.503 回答
0

为了在执行命令后保持 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-12-01T13:40:38.823 回答
0

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

于 2016-10-08T19:41:28.587 回答