PowerShell Implicit Remoting: Never Install a Module Again

Don Jones

Thu, 2010-05-27 10:11

Here's a scenario: You're running Windows XP on your admin workstation. You've installed PowerShell v2, and you want to use some of the cool new cmdlets that ship with Windows Server 2008 R2 - like the ActiveDirectory module. But that module won't run on XP, not even a little. So are you forced to upgrade to Windows 7? Well, that wouldn't be a bad thing - I mean, XP is so eight years ago - but you do have other options.

You'll need a Win2008R2 machine somewhere in your environment, with the ActiveDirectory module (or whatever module you're after) installed on it. Enable remoting on that computer by running Enable-PSRemoting on it. Let's say the computer's name is Win2008R2.

From your XP workstation, run this:

$s = new-pssession -computer "Win2008R2"

This establishes a remoting session to the computer. Now, tell it to load the module you want to utilize:

Invoke-Command -session $s -script { Import-Module ActiveDirectory }

Now, here's the magic part, called Implicit Remoting: Import the commands from that module into your local shell, giving them a prefix to help you remember that they're remote (the prefix bit is optional, but I like it):

Import-PSSession -session $s -module ActiveDirectory -prefix Rem

Now, so long as that session remains active, you can run the commands from that module as if they were local:

Get-RemADUser -filter * -searchbase "cn=users,dc=company,dc=pri"

Just add the "Rem" prefix to the cmdlets' noun. The command will feel like it's running locally, but in fact it'll be running on the remote server, and the results will be transmitted over the wire to your computer. When you're all done:

$s | Remove-PSSession

And the connection will close, and the cmdlets will no longer be available. This is a great trick for using modules that exist on other computers, but which cannot be installed on your workstation. In fact, someday we might live in a world where we NEVER install management tools on our workstations. Tools install on a server when you install the related role (like the ActiveDirectory module being installed when you install Active Directory Domain Services on a server). We'll just consume the management tools as a service, remotely. Someday, those modules might even register a record in DNS, so that we could just type a single command and be connected to a server that can provide us with the module we want. Wouldn't that be cool?