Chapter 4: Interactive remoting sessions
To understand the advantages of interactive remoting in PowerShell 2.0, let us first look at some gotchas with Invoke-Command. Take an example of a remote system where SharePoint 2010 is installed. SharePoint 2010 provides native PowerShell cmdlets and these cmdlets can be accesses only if you load Microsoft.SharePoint.PowerShell PS snap-in. So, to do this using Invoke-Command
$s = New-PSSession -ComputerName SP2010-WFE
#load the PS Snap-in to enable SharePoint PS cmdlets
Invoke-Command -Session $s -ScriptBlock {Add-PSSnapin Microsoft.SharePoint.PowerShell}
#$s has the PowerShell cmdlets now
Invoke-Command -Session $s -ScriptBlock {Get-SPWeb http://sp2010-wfe:999}
If you look at the above code, we will have to use a persistent session so that we can use SharePoint cmdlets in subsequent Invoke-Command calls. Without a persistent session, you will have to load the SharePoint snap-in every time before using a SharePoint cmdlet.
Another caveat will be the unavailability of remote computer cmdlets in the local PowerShell session — in this case, the SharePoint 2010 cmdlets. This — essentially – means that we cannot use Get-Help or Get-Command cmdlets against the SharePoint 2010 cmdlets in the local session unless we pass that as a script block to Invoke-Command.
One more disadvantage of using Invoke-Command is unavailability of command completion. Unless the cmdlet you are using inside the script block is available locally, you cannot use tab completion. This can be a pain for many, including me.
This is where interactive remoting comes in to play.
Starting an interactive remoting session
Enter-PSSession and Exit-PSSession are the cmdlets used to start / exit an interactive remoting session.
To enter an interactive session,
Enter-PSSession -ComputerName Ravi-Dev
Once you enter an interactive remoting session, PowerShell prompt changes to reflect the remote computer name you just connected to. This indicates that you are in an interactive remoting session.
Figure 5 Interactive Session
You can now add the SharePoint snap-in using Add-PSSnapin cmdlet.
Add-PSSnapin Microsoft.SharePoint.PowerShell
Once the snap-in is loaded, you will have access to all the SharePoint 2010 cmdlets as if they are available on the local computer. You can verify that by using Get-Help against one of the SharePoint 2010 cmdlets.
Get-Help Get-SPWeb –Full
Exiting an interactive session
You can use Exit-PSSession to come out of an interactive PS Session. Remember, by specifying – ComputerName as the parameter to Enter-PSSession, we are using only a temporary PS Session and is not a persistent session. So, any variables you create and the command history will all be gone if you exit this interactive session.
Using persistent sessions with interactive remoting
As discussed earlier, it will be advantageous to use persistent sessions. By using a persistent session, you can enter and exit the interactive session as many times as you like. All the data and variables you created in the remote session will persist until you remove the session. You can do it the same way you used persistent sessions with Invoke-Command.
$s = New-PSSession -ComputerName SP2010-WFE
Enter-PSSession -Session $s
Starting interactive remoting with an existing session
It is quite possible that you would have created a persistent session to use with Invoke-Command. You can use the same persistent session with Enter-PSSession to start an interactive remoting session.
You can use Get-PSSession cmdlet to see a list of all available/opened PS Sessions and then use EnterPSSession as shown above to start interactive remoting. As you see here, I will pipe Get-PSSession output to Format-List cmdlet to get all session details.
Figure 6 Get-PSSession
There are four ways to enter an existing PS Session for interactive remoting. I have highlighted the available options in the above screenshot. You can use whichever way is convenient to you.
Method 1: Using session Id
Enter-PSSession -id 1
Method 2: Using session instance Id
Enter-PSSession -InstanceId 55a417ed-f903-4265-a4dc-c892c2500e0d
Method 3: Using session name
Enter-PSSession -Name Session1
Method 3: Using –session parameter
$s = Get-PSSession -Id 1
Enter-PSSession -Session $s
All of the above options start interactive session using the persistent session “session1″. It is just more than one way to do the same thing.
Summary
This chapter gave a quick overview of interactive remoting in PowerShell 2.0 and how to use EnterPSSession, Exit-PSSession and Get-PSSession cmdlets.