powershell - 通过调用命令启 Action 业 : Job is only available in remote session

标签 powershell

我正在尝试通过以下命令在不同的服务器上启动 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/

相关文章:

powershell - token '&&' 在 vs 代码终端的这个版本中不是有效的语句分隔符

Powershell目录比较

django - Django Runserver因自动重载而崩溃?

powershell - 如何让 Windows Powershell Get-ItemProperty 只显示我想要的属性?

azure - Azure CLI 和 Powershell cmdlet 之间的功能不一致

c# - 使用 c# 程序的参数调用远程 powershell 脚本

PowerShell 模块 list - 自动加载所需模块

powershell - 哈希表数组未输出

powershell - 标准输入到 powershell 脚本

powershell - 在PowerShell中写入时锁定文件