Start-Job via Invoke-Command: the job is only available on a remote session
I am trying to run TShark on different servers using the following command:
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"}
The team is working and everything is fine. But when I connect to a remote server via, for example, remote desktop and execute the command, Get-Job
nothing is returned. Thus, this means that the initial work is only started in advance in the remote session. Does anyone know if there is a way to get started in the "global" realm. I don't want TShark to stop tracking if I accidentally close the PowerShell window.
thank
source to share
You are correct that this job only exists during a PowerShell host session, so you cannot get it when you RDP.
A better approach might be to use a switch -AsJob
Invoke-Command
. Then you can get the results by running Get-Job
on your local machine, not RDP on the remote host:
Invoke-Command -Session $remoteSession -ScriptBlock { & 'C:\Program Files\Wireshark\tshark.exe' -b filesize:10000 -b files:5 -w "$tsharkResultDirectory\tshark.pcap" } -AsJob -Name "TShark"
Note that you will lose the job if you end your local PowerShell session.
I believe the only way to start a remote job and save it is with * -ScheduledJob commands when they write their results to disk:
PS C:\> get-command *ScheduledJob | Select Name
Name
----
Disable-ScheduledJob
Enable-ScheduledJob
Get-ScheduledJob
Register-ScheduledJob
Set-ScheduledJob
Unregister-ScheduledJob
source to share