Disconnected sessions

The InDisconnectedSession of Invoke-Command starts the requested script and immediately disconnects the session. This allows a script to be started and collected from a different console session or a different computer.

The session parameter cannot be used with InDisconnectedSession; Invoke-Command creates a new session for a specified computer name. The session is returned by the following command:

Invoke-Command { Start-Sleep -Seconds 120; 'Done' } -ComputerName PSTest -InDisconnectedSession 

A second PowerShell session or computer is able to connect to the disconnected session to retrieve the results. The following command assumes that only one session exists with the PSTest computer:

Get-PSSession -ComputerName PSTest | 
    Connect-PSSession | 
    Receive-PSSession 

Tasks started with AsJob will also continue to run if a session is disconnected. The following example creates a session, starts a long-running process, and disconnects the session:

$session = New-PSSession PSTest -Name 'Example' 
Invoke-Command { Start-Sleep -Seconds (60 * 60) } -Session $session -AsJob 
Disconnect-PSSession $session 

Once the session has been created and disconnected, the PowerShell console can be closed. A second PowerShell console can find and connect to the existing session:

$session = Get-PSSession -ComputerName PSTest -Name 'Example' 
Connect-PSSession $session 

Reviewing the details of the session will show that it is busy running Start-Sleep:

PS> Get-PSSession | Select-Object Name, ComputerName, State, Availability

Name ComputerName State Availability
---- ------------ ----- ------------
Example PSTest Opened Busy
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.0.157