Windows powershell screen equivalent
I am using a windows machine as a ssh server. I want to remotely run some tasks which takes several hours to finish. Is there any way to have persistent process even after remote ssh session is disconnected in Powershell?
After some experimentation, now I have replaced ssh with PowerShell Remoting.
Especially, START A REMOTE JOB THAT KEEPS THE RESULTS ON THE REMOTE COMPUTER section of the about_Remote_Jobs has been really helpful.
Basically, my current workflow is
- Open a new PSSession
- Invoke a command (
Invoke-Command
) on the remote server as a job, withStart-Job
Then I can freely Disconnect from the session and later reconnect to the session and get the job outputs with Receive-Job
.
Use the RunasJob parameter via a PSSession:
(from Help about_remote_jobs)
START A REMOTE JOB THAT RETURNS THE RESULTS TO THE LOCAL COMPUTER (ASJOB)
To start a background job on a remote computer that returns the command results to the local computer, use the AsJob parameter of a cmdlet such as the Invoke-Command cmdlet.
When you use the AsJob parameter, the job object is actually created on the local computer even though the job runs on the remote computer. When the job is completed, the results are returned to the local computer.
You can use the cmdlets that contain the Job noun (the Job cmdlets) to manage any job created by any cmdlet. Many of the cmdlets that have AsJob parameters do not use Windows PowerShell remoting, so
you can use them even on computers that are not configured for
remoting and that do not meet the requirements for remoting.STEP 1: INVOKE-COMMAND -ASJOB
The following command uses the AsJob parameter of Invoke-Command to start a background job on the Server01 computer. The job runs a Get-Eventlog command that gets the events in the System log. You can use the JobName parameter to assign a display name to the job.
invoke-command -computername Server01 -scriptblock {get-eventlog system} -asjob
The results of the command resemble the following sample output.
SessionId Name State HasMoreData Location Command --------- ---- ----- ----------- -------- ------- 1 Job1 Running True Server01 get-eventlog system
When the AsJob parameter is used, Invoke-Command returns the same type of job object that Start-Job returns. You can save the job object in a variable, or you can use a Get-Job command to get the job.
Note that the value of the Location property shows that the job ran on the Server01 computer.
STEP 2: GET-JOB
To manage a job started by using the AsJob parameter of the Invoke-Command cmdlet, use the Job cmdlets. Because the job object that represents the remote job is on the local computer, you do not need to run remote commands to manage the job.
To determine whether the job is complete, use a Get-Job command. The following command gets all of the jobs that were started in the current session.
get-job
Because the remote job was started in the current session, a local Get-Job command gets the job. The State property of the job object shows that the command was completed successfully.
SessionId Name State HasMoreData Location Command --------- ---- ----- ----------- -------- ------- 1 Job1 Completed True Server01 get-eventlog system
STEP 3: RECEIVE-JOB
To get the results of the job, use the Receive-Job cmdlet. Because the job results are automatically returned to the computer where the job object resides, you can get the results with a local Receive-Job command.
The following command uses the Receive-Job cmdlet to get the results of the job. It uses the session ID to identify the job. This command saves the job results in the $results variable. You can also redirect the results to a file.
$results = receive-job -id 1
What you are looking for are PSSessions.
From the about_pssessions page on technet (http://technet.microsoft.com/en-us/library/hh847839.aspx)
However, unlike the session that starts automatically, you can control the PSSessions that you create. You can get, create, configure, and remove them, disconnect and reconnect to them, and run multiple commands in the same PSSession. The PSSession remains available until you delete it or it times out.
Since the OP is using ssh to connect to Powershell from a non-Windows machine, he can't use Powershell sessions. He would be running it as though it were connected to the local machine. The Start-Job cmdlet should do the trick:
Start-Job -scriptblock {script or command you wish to run as job}