Implicit Remoting in Windows PowerShell

Implicit Remoting in Windows PowerShell

Implicit Remoting is a way that adds proxy commands to your local PowerShell session. The proxy commands are functions that look, feel and behave like local cmdlets in the session. When you run a command in your session, instead of running the command on the local computer, the proxy command runs the real command as a session on the remote computer and returns the results to the local session.

In this way you do not need to install roles or features on your local computer. For example, if you want to manage Active Directory from your own computer, you would be installing Remote Server Administration Tools (RSAT) on local computer. But now you can use Windows PowerShell Implicit Remoting feature to Manage Active Directory without having to install RSAT on local computer. The good news is that Implicit Remoting is supported in Windows PowerShell v 2.0 and above.

Module Creation

First of all, establish a remote session to your domain controller:

$Session = New-PSSession -ComputerName DC1

Of course replace the “DC1” with your own domain controller name. You can specify additional parameters, such as alternate credentials or alternate WinRM ports.

Next, Ask the remote instance of Windows PowerShell to load the Active Directory cmdlets:

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

Now comes the cool part. Export the Active Directory cmdlets to your local instance of Windows PowerShell from the remote session into a local module on your computer:

Export-PSSession -Session $Session -CommandName *-AD* -OutputModule RemAD -AllowClobber

This command creates a new Windows PowerShell module in your Documents folder under WindowsPowerShell\Modules\RemAD. Only the cmdlets with names matching the pattern “*-AD*” will be included.

The cmdlets are not actually copied to your local computer. Instead, the locally created module serves as a kind of shortcut. The cmdlets will always be executed on the remote domain controller, but the cmdlets will seem to be running locally.

Using the Cmdlets

Load the newly created module:

Import-Module RemAD

This command loads the new module into memory. If you don’t want to manually import the module every time you open the PowerShell session, you can add “Import-Module RemAD” command to your PowerShell profile. For more information about PowerShell profiles, visit Windows PowerShell Profiles page or watch this video.

To make sure you have got all the commands to manage Active Directory, enter the following command:

Get-Command -Module RemAD

This command lists all the cmdlets available under this module. This will include all the commands related to managing your Active Directory.

Use help on any cmdlet:

Get-Help Get-ADUser

You’ll see the following error:

Get-Help : Cannot get Help from a remote runspace, because the runspace has not been opened.  Open the runspace by running 
an implicit remoting command, and then try running the command to get Help again.

At line:55 char:5

+     Get-Help @PSBoundParameters | more

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-Help], InvalidOperationException

    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetHelpCommand

 This error occurs because a current remoting session isn’t established between your computer and the domain controller where these cmdlets live. You don’t need to explicitly start that session. You can implicitly do so by trying to execute one of the remoted cmdlets:

Get-ADUser -Filter "Name -like 'A*'"

This will re-initiate the remote connection to the domain controller, submit the command for execution and run the command on the domain controller. Then every user whose name starts with “A” will be serialized into XML and transmitted across the network to your computer. On local computer, the XML objects will be de-serialized back into PowerShell objects you can work with. Now you can use help commands because the remote session is active:

Get-Help Get-ADUser

The session remains active until you close the PowerShell instance or remove the module using following command:

Remove-Module RemAD

In the similar manner, you can manage Microsoft Exchange, Microsoft Azure or any other platform by running any cmdlet that is actually installed in remote server but you will feel like running on your local computer.

Leave a Comment