PowerShell Remoting
157Implicit Remoting
10.11 Implicit Remoting
Implicit Remoting is an incredibly cool trick and one that you’ll get more and more use out of in the future. The basic idea is this: Rather than installing every possible PowerShell module on your computer, you leave the modules installed out on servers.
You can then “import” the modules into your current PowerShell session, making it look like the commands in the modules all live locally. In reality, your computer will contain “shortcuts” to the commands, and the commands will execute out on the serv-ers you got them from. The results—and even the commands’ help—will be brought to your computer via Remoting.
Here’s an example, where you’ll import the ServerManager module from a remote server:
PS C:\> $sess = New-PSSession -ComputerName win8
PS C:\> Invoke-Command -Session $sess -ScriptBlock { Import-Module
➥ servermanager }
PS C:\> Import-PSSession -Session $sess -Module ServerManager -Prefix RemSess ModuleType Name ExportedCommands
--- ----
---Script tmp_1hn0kr5w.keb {Get-WindowsFeature, Ins...
Here’s what you did:
1 You opened a session to the remote machine, saving the session object in a vari-able for easy use later.
2 You invoked a command against that session, asking it to load the desired mod-ule into memory.
3 You imported that session, grabbing only the commands in the ServerManager module. To make these commands easy to distinguish, you added the prefix
“RemSess” to the noun of all imported commands. The prefix is optional but is recommended especially if you are importing to a Windows 8 or Windows Server 2012 system with the greatly increased number of cmdlets.
You can quickly check to see which commands you brought over:
PS> Get-Command -Noun RemSess*
CommandType Name --- ----
Alias Add-RemSessWindowsFeature Alias Remove-RemSessWindowsFeature Function Disable-RemSessServerManagerStandardUserRemoting Function Enable-RemSessServerManagerStandardUserRemoting Function Get-RemSessWindowsFeature Function Install-RemSessWindowsFeature Function Uninstall-RemSessWindowsFeature
NOTE The module name column has been removed to enable the display to fit the page width
You can now run these commands, just as if they were locally installed, and can even access their help (provided the server has had Update-Help run so that it has a copy of the help locally). The only caveat is the one that applies to all results in Remoting:
The results of your commands won’t have any methods attached to them, because the results will have been through the serialization/deserialization process.
These “imported” commands will exist as long as your session to the remote machine is open and available. Once it’s closed, the commands will vanish. If you want to make these commands always available to you, then save the remote session infor-mation to a module using the Export-PSSession cmdlet.
There are a few ways you might want to use this. First, take your current session and export everything to a module:
PS C:\> Export-PSSession -Session $q -OutputModule QuarkAll
The session $q is to the computer named Quark. This command will create a module called QuarkAll under $home\Documents\WindowsPowerShell\Modules:
PS C:\> Get-Module -ListAvailable QuarkAll
ModuleType Name ExportedCommands --- ---- ---Manifest QuarkAll {}
Later, you can import this module as you would with implicit Remoting. Because the imported cmdlet names may conflict, add a prefix:
PS C:\> Import-Module QuarkAll -Prefix Q
The first time you try to run one of the commands, PowerShell dynamically creates the necessary session and establishes a remote connection:
PS C:\> Get-Qsmbshare
Creating a new session for implicit remoting of "Get-SmbShare" command...
If you check sessions, you should see a new one created for this module:
PS C:\> Get-PSSession | select * State : Opened ComputerName : quark
ConfigurationName : Microsoft.PowerShell
InstanceId : 662484ed-d350-4b76-a146-865a8d43f603 Id : 2
Name : Session for implicit remoting module at
C:\Users\Jeff\Documents\WindowsPowerShell\Modules\
QuarkAll\QuarkAll.psm1 Availability : Available
ApplicationPrivateData : {PSVersionTable}
Runspace : System.Management.Automation.RemoteRunspace
If you remove the module, the session is also automatically removed.
159 Summary
You can also create a limited module by only exporting the commands you want.
First, create a session:
PS C:\> $q=New-PSSession Quark
Then, create a new module exporting only the Get cmdlets:
PS C:\> Export-PSSession -Session $q -OutputModule QuarkGet -CommandName Get*
➥ -CommandType cmdlet
When you import the module, the only commands you can run remotely on Quark are the Get cmdlets:
PS C:\> Import-Module QuarkGet -Prefix Q PS C:\> Get-Command -module QuarkGet
CommandType Name Definition - ---- ---Function Get-QAppLockerFileInformation ...
Function Get-QAppLockerPolicy ...
Function Get-QAppxProvisionedPackage ...
Function Get-QAutoEnrollmentPolicy ...
Function Get-QBitsTransfer ...
...
One thing we should point out is that when you export a session, any commands with names that might conflict on your local computer are skipped unless you use the -AllowClobber parameter. In the examples with Quark, you’re actually connecting from a computer running PowerShell 2.0 to one running PowerShell 3.0 and thus are able to use the 3.0 cmdlets just as if they were installed locally:
PS C:\> get-qciminstance win32_operatingsystem | Select
➥ CSName,BuildNumber,Version
Creating a new session for implicit remoting of "Get-CimInstance" command...
CSName BuildNumber Version --- ---- ---QUARK 8250 6.2.8250
Implicit Remoting is an incredibly powerful technique that lets you take advantage of modules, snap-ins, and tools that you may not have installed locally. If you find your-self needing these tools often, take the time to export a session to a module; then you’ll be ready for anything.
10.12 Summary
Remoting was the most eagerly awaited feature in PowerShell v2. It moved Power-Shell’s capabilities up by several levels. You can gain remote access to systems through a number of cmdlets that have a –ComputerName parameter or through the WSMAN -based Remoting technology.
Once you’ve mastered the material in this chapter, you’ll be able to administer all of the machines in your environment from the comfort of your own workstation.
160