我正在尝试通过以下命令在不同的服务器上启动 TShark:
Invoke-Command -Session $remoteSession -ScriptBlock {start-job -ScriptBlock {& 'C:\Program Files\Wireshark\tshark.exe' -b filesize:10000 -b files:5 -w "$tsharkResultDirectory\tshark.pcap"} -Name "TShark"}
命令有效,一切正常。但是当我通过例如连接到远程服务器时远程桌面,并执行命令 Get-Job
没有任何返回。
因此,这意味着预先启动的作业仅在远程 session 中运行。有谁知道是否有办法在“全局”范围内开始工作。如果我不小心关闭了 PowerShell 窗口,我不希望 TShark 停止跟踪。
谢谢
最佳答案
您是正确的,该作业仅在 PowerShell 主机 session 期间存在,这就是您在 RDP 时无法检索它的原因。
更好的方法可能是使用 Invoke-Command
的 -AsJob
开关。然后,您可以通过在本地计算机上运行 Get-Job
来检索结果,而不是通过 RDP 连接到远程主机:
Invoke-Command -Session $remoteSession -ScriptBlock { & 'C:\Program Files\Wireshark\tshark.exe' -b filesize:10000 -b files:5 -w "$tsharkResultDirectory\tshark.pcap" } -AsJob -Name "TShark"
再次注意,如果您结束本地 PowerShell session ,您将失去这份工作。
我相信运行远程作业并让它持久化的唯一其他方法是通过 *-ScheduledJob 命令,因为它们将结果记录在磁盘上:
PS C:\> get-command *ScheduledJob | Select Name
Name
----
Disable-ScheduledJob
Enable-ScheduledJob
Get-ScheduledJob
Register-ScheduledJob
Set-ScheduledJob
Unregister-ScheduledJob
关于powershell - 通过调用命令启 Action 业 : Job is only available in remote session,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44884550/