A Layman's Guide to PowerShell 2.0 Remoting by Revikanth Chaganti & Jan Egil Ring - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Chapter 6: Saving remote sessions to disk

In chapter 5, we looked at how we can use Import-PSSession cmdlet to execute remote commands as if they were local. This is nice but this will last only while the persistent session is alive. The moment we kill the session — using Remove-PSSession or the session is broken; the implicit remoting session will also get killed. 

In this chapter, we look at how we can save a remoting session to disk so that we don’t even have to explicitly create a PS session to execute commands on a remote computer.

Export remote session to a module on disk

This is achieved using Export-PSSession cmdlet. This cmdlet lets us import commands from a remote session and save the same in a PowerShell module on the local disk. This cmdlet can get cmdlets, functions, aliases, and other command types in to a PowerShell module. The following example shows how we can achieve this. 

$s = New-PSSession -ComputerName SP2010-WFE

Invoke-Command -Session $s -ScriptBlock {Import-Module ActiveDirectory}

Export-PSSession -Session $s -OutputModule ADRemoteCommands -AllowClobber -Module ActiveDirectory

In the above example, the first two lines should be quite familiar by now. The third line is where the magic happens. We tell Export-PSSession cmdlet to export all the commands, aliases, functions, etc available in PS Session $s to a module on hard disk and name it ADRemoteCommands. 

If the Export-PSSession is successful, you will see output similar to what is shown here

img8.png

Figure 7 Export-PSSession output

In the above output, it is clear that Export-PSSession generates .psm1, .psd1 and format data file for the module automatically. Now, you can load the module at any later point in time to get access to the remote commands.

Importing a module saved on disk

If you observe the output closely, path where the module files are stored is same as $Env:PSModulePath.

So, you don’t need to specify the absolute path to the module. 

Import-Module ADRemoteCommands

This imports all remote commands available in the module to local session. Whenever we execute a remote command, implicit remoting kicks in, establishes the remote session, executes the command in remote session and returns the output. All this is done without you really using any remoting related cmdlets. If establishing a remote session requires a password, you will be prompted for one.

Limitations of Export-PSSession

Using Export-PSSession has the same limitations as implicit remoting. You cannot use Export-PSSession to export a Windows PowerShell provider. You cannot start a program with user interface or requires access to interactive desktop. The exported module does not include the session options used to create the session. So, if you need any specific session options to be configured before running remote commands, you need to create a PS Session with all the required session options before importing the on disk module.

Summary

Saving a remote session to disk can be done using Export-PSSession cmdlet. This is a very quick method to execute commands on a remote computer without explicitly creating a PS session or entering an interactive remoting session. 

This also brings us to the end of part 1. In part 1, we looked at the basics of PowerShell remoting such as what is remoting, enabling remoting in various scenarios, executing remote commands and importing/exporting remoting sessions.  Part 2 of this guide looks at a bit more advanced aspects of PowerShell remoting.

Keep reading..!