PowerShell implicit remoting
What is implicit remoting?
This is a feature of PowerShell that automatically executes a command on a different computer. When you import commands from a remote session, PowerShell will execute them on the remote computer and return the results to your computer, in otherwords they’re implicity remoted to the remote computer.
The only caveat with implicit remoting is that the results returned to your computer won’t have any methods that you can interact with because the objects properties are copied into an XML file for transmission over the network.
Saving modules from another session on your local computer using Export-Psession
I normally use export-pssession when I want to save commands from another computer on to my computer. It saves the commands in a module and they are not imported into the current session. I usually do this if they are commands I will use a lot.
$session = New-PSSession -ComputerName server01
Invoke-Command -Session $session -ScriptBlock {import-module VirtualMachineManager}
Export-PSSession -Session $session -CommandName *-scvirtualmachine* -OutputModule RemSC -AllowClobber
Import-Module RemSCIn the above example I export commands to a module folder called remSC. This way I can just run the command when I need to from my computer and it will use the command from the remote computer. It will create a remote session on the fly whenever I use any command from the remSC module. I also specified which commands I wanted from that session using the CommandName parameter.
Import commands from another PSSession into the current session
The Import-PsSession is used to import commands from a customized shell, such as Exchange or snap-ins that are not in the current session, for example:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchange01.corp.ordobott.net/PowerShell/ -Authentication Kerberos
Import-PSSession $Session -DisableNameChecking
Remove-PSSession $SessionYou would be able to use the commands from the remote session using the implicit remoting feature. When you remove the session using Remove-PSSession the imported commands are no longer available.
Prefix parameter
In order to keep track of remote commands, it’s a good idea to add a prefix to them when you import them. It also avoids name conflicts with existing commands in the session. So for example if you import Get-ADComputer, the cmdlet will be known as Get-remAdComputer.
$session = New-PSSession dc01
Import-PSSession -Session $session -Module activedirectory -Prefix rem
#Output:
ModuleType Version PreRelease Name ExportedCommands
---------- ------- ---------- ---- ----------------
Script 1.0 tmp_oo3ay31k.hfn {Add-remADCentralAccessPolicyMember, Add-remADComputer…
Get-remADComputer -Identity workstation01
Remove-PSSession $SessionIn the above example you can see that it creates a temporary module and the commands have rem added to the noun part of the cmdlet.
