Tome's Land of IT

IT Notes from the Powertoe – Tome Tanasovski

Receiving a Disconnected PowerShell Session Asjob

I was down in Washington DC delivering a presentation about Windows Server 2012 for a roadshow that Microsoft put on. This was one stop in a few that I was lucky enough to be a part of (actually it’s not over yet – Boston is coming up on Wednesday). During that presentation I was showing off how you can disconnect a PowerShell session and then receive that session from another computer. This is a great new feature in PowerShell remoting that is integral to the new movement in Server to use PowerShell or tools that leverage PowerShell like the new server manager as the management tool of choice.

The demo goes like this:

Setup

3 computers

Server1 – The computer I am starting on. Windows Server 2012 RC1 (with PowerShell v3)
Server2 – The computer I am connecting to. Windows Server 2012 RC1 (with PowerShell v3)
Server3 – The computer I will use to connect to Server2 after Server1 is disconnected. Winows Server 2012 RC1 (with PowerShell v3)

The following script gets run on Server1 to start it off:

# Create a remote session
$s = New-PSSession -ComputerName server2
$s
# Start a long running command as a job on the session
$job = Invoke-Command $s {1..10000 | % {sleep 1; "Output $_"}} -AsJob
$job

After the job runs for a few seconds, you run the following on Server1 to disconnect the session:

Disconnect-PSSession $s

You can then close PowerShell on Server1 and open it on Server3. The following command will show you what sessions are available on Server2:

Get-PSSession -Computername Server2

The demo is finalized by performing the following to get into the session that was disconnected on Server1:

Get-PSSession -Computername Server2 |Receive-Session

This is a great demo that shows one of the best new features in the new version of the management framework. The only downside with the above is that the command started as a job, but when you call receive-pssession you are placed in the middle of the session as if you typed enter-pssesion. The problem with this is that ctrl-c will now break your running process. The natural question that was posed during the demonstration was, “How do you run receive-pssession, but keep it as a job?” My first inclination was to see if receive-pssession had an -asjob parameter. The answer is no. The solution is rather simple, but it did throw me for a loop. So much so that I thought I would share.

Solution

If you want to call receive-pssession asjob, simply run start-job with receive-pssession:

$job = Start-Job -ScriptBlock {Get-PSSession -ComputerName Server2|Receive-PSSession}

I told you it was simple, and I’m sure plenty of you who are reading this had figured this out without having to read the article. However, I thought it was worth discussing. If nothing else, this article at least highlights one of the great new features in PowerShell v3 Remoting.

Updated Solution – 6/26/2012

Thanks to Steve and Andreas in the comments (they require approval so I read them at the same time before they were visible on the page), there is a parameter in Receive-PSSession called OutTarget.  You can specify the following to force a Receive-PSSession to return a job:

$job = Get-PSSession -Computername Server2 |Receive-PSSession -OutTarget Job

I don’t like this.  I don’t like this so much that I have filed a connect suggestion to make Receive-PSSession use the more familiar -ASJob parameter.  Feel free to vote it up, if you agree.  Now, with that distraction, I hope no one noticed that I absolutely did not read the full Get-Help before making this post.  <ahem> wait…. I’ll come up with an excuse eventually.  All kidding aside, thanks for reading!  And thank you for helping make the site accurate!

Advertisements

6 responses to “Receiving a Disconnected PowerShell Session Asjob

  1. Steven Murawski June 25, 2012 at 3:48 pm

    Hey Tome,
    Why not use
    Get-PSSession -Computername Server2 | Receive-PsSession -outtarget Job
    ?
    Receive-PSSession will default to host, but you can return the result to a job as well.

  2. Andreas June 26, 2012 at 3:44 am

    Hi,

    You can use the OutTarget parameter…
    $Job = Get-PSSession -ComputerName Server2 | Receive-PSSession -OutTarget Job

    Thanks for a great blog.
    /Andreas

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: