To understand the details of implicit remoting and how it works, let’s look into the sample scenario given below.
What I’ve done here is imported a module (PSSQL) using a session that was created before and then imported that session to the local computer with the module PSSQL so that I can use the commands that are available as part of that module on my computer. To easily identify the commands that are imported as part of the Import-PSSession I’ve prefixed the commands REMSQL.
Now I can execute the cmdlets that are listed by the
Get-Command –Noun REMSQL* command in the screenshot as if I execute any local
commands. Please note that the behavior of the execution is the same as for
other commands that are executed remotely. The objects that are returned as
part of the execution will not have any methods attached to them as they are
actually serialized objects from the remote machine. If you have imported a
module from a computer, you need to remember that the commands run on the
remote computer. If you run Get commands, they will get data from the remote
computer. If you run Set commands, they change data on the remote computer. The
commands look and feel local, but they're remote commands.
Persisting the imported commands
The commands that you have imported using Import-PSSession
option will only stay in the local computer till the session is active. That
means as soon as the remote session is closed or exited, you'll lose the
commands. If you still want to keep the imported commands for later use even
after the session is closed, then you need to persist the commands on the local
machine.
The Export-PSSession cmdlet will help you persist the
imported commands from another session and saves them in a Windows PowerShell
module.
Unlike Import-PSSession, which imports commands from another
PSSession into the current session, Export-PSSession saves the commands in a
module. The commands are not imported into the current session. By default,
Export-PSSession exports all commands, except for commands that exist in the
current session, but you can use the CommandName parameters to specify the
commands to export.
The Export-PSSession cmdlet uses the implicit remoting
feature of Windows PowerShell. When you import commands into the current
session, they run implicitly in the original session or in a similar session on
the originating computer.
For e.g the above command will export the commands available
in the module SQLPS to a module REMSQLPS on the local machine and can be used
later for execution. Export-PSSession command support multiple arguments that can
be used to customize the export, like –CommandName, -CommandType etc. Once you have exported the commands and persisted on the
local computer, you can later use the Import-Command to load these modules whenever
needed and work on the commands like any other powershell commands. While
executing these commands, PowerShell will take care of creating a new remote
session and managing it.
No comments:
Post a Comment