{"id":3241,"date":"2009-12-29T15:09:59","date_gmt":"2009-12-29T15:09:59","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/12\/29\/saving-remote-session-to-your-local-disk\/"},"modified":"2019-02-18T13:06:03","modified_gmt":"2019-02-18T20:06:03","slug":"saving-remote-session-to-your-local-disk","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/saving-remote-session-to-your-local-disk\/","title":{"rendered":"Saving remote session to your local disk"},"content":{"rendered":"<p>Read the previous <a href=\"http:\/\/blogs.msdn.com\/powershell\/archive\/2009\/12\/29\/bringing-remote-commands-to-your-local-session.aspx\">post on implict remoting<\/a> to learn how the <a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkID=135221\">Import-PSSession<\/a> cmdlet makes it easier to work with remote commands by presenting them as if they were local commands. This user experience saves you the trouble of typing long <a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkID=135225\">Invoke-Command<\/a> incantations to pass <a href=\"http:\/\/blogs.msdn.com\/powershell\/archive\/2009\/12\/29\/arguments-for-remote-commands.aspx\">arguments to remote commands<\/a> or to download remote help content. <\/p>\n<p>The next great thing would be to jump straight into the implicit remoting experience without having to explicitly set up a remote session each time and having to remember how to invoke Import-PSSession&#8230; This is where <a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkID=135213\">Export-PSSession<\/a> cmdlet comes handy &#8211; it can be used to save the remote session and the remote commands to a local disk.<\/p>\n<h3>Temporary implicit remoting modules<\/h3>\n<p>Let&#8217;s <a href=\"http:\/\/blogs.msdn.com\/powershell\/archive\/2009\/12\/29\/bringing-remote-commands-to-your-local-session.aspx\">recall<\/a> how one can import remote commands into a local session:<\/p>\n<pre>PS&gt; $s = New-PSSession -ComputerName lukasza5 -Credential REDMOND\\lukasza\nPS&gt; Import-PSSession -Session $s -CommandName *-Process -Prefix Remote\nModuleType Name                      ExportedCommands\n---------- ----                      ----------------\nScript     tmp_a50e3c88-46f1-4c25... {Stop-Process, Get-Process, Debug-Process, Wait-Process...}<\/pre>\n<p>Import-PSSession cmdlet creates a temporary module containing local functions that act as <a href=\"http:\/\/blogs.msdn.com\/powershell\/archive\/2009\/01\/04\/extending-and-or-modifing-commands-with-proxies.aspx\">proxies<\/a> for remote commands. The module is then implicitly imported into the local session by the Import-PSSession cmdlet. The module and all the temporary files are deleted whenever the user explicitly removes the module or when the remote session is closed:<\/p>\n<pre>PS&gt; Remove-PSSession $s<\/pre>\n<h3>Saving an implicit remoting module<\/h3>\n<p>Instead of working with temporary <a href=\"http:\/\/blogs.msdn.com\/powershell\/archive\/2008\/08\/10\/all-about-modules.aspx\">modules<\/a> created by Import-PSSession, one can save a module in the file system. This is done one with the Export-PSSession cmdlet. Example below asks Export-PSSession cmdlet to look in the remote session $s, take all the remote commands matching &quot;*-Process&quot; wildcard, and save them to &quot;MyRemoteCommands&quot; module. The example explicitly says that it would be okay to clobber local commands that have the same name as the imported, remote commands.<\/p>\n<pre>PS&gt; $s = <a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkID=135237\">New-PSSession<\/a> -ComputerName lukasza5 -Credential REDMOND\\lukasza\nPS&gt; <a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkID=135213\">Export-PSSession<\/a> -Session $s -CommandName *-Process -OutputModule MyRemoteCommands -AllowClobber\n    Directory: C:\\Users\\lukasza\\Documents\\WindowsPowerShell\\Modules\\MyRemoteCommands\nMode                LastWriteTime     Length Name\n----                -------------     ------ ----\n-a---        2009-12-29  11:50 AM      20535 MyRemoteCommands.psm1\n-a---        2009-12-29  11:50 AM         99 MyRemoteCommands.format.ps1xml\n-a---        2009-12-29  11:50 AM        598 MyRemoteCommands.psd1\nPS C:\\&gt; <a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkID=135250\">Remove-PSSession<\/a> $s<\/pre>\n<p>You can see that Export-PSSession cmdlet saved a new module under the default user path from ${env:PSModulePath}. No remote commands have been imported into the local session yet &#8211; there is no Get-RemoteProcess command and Get-Process works against the local machine.<\/p>\n<h3>Importing a saved implicit remoting module<\/h3>\n<p>After implicit remoting module is saved, one can invoke the remote commands without having to ever again use New-PSSession, Invoke-Command, Import-PSSession or Export-PSSession. Let&#8217;s see how that works: <\/p>\n<pre>PS&gt; Import-Module MyRemoteCommands -Prefix Remote<\/pre>\n<p>In the example above, I imported the remote commands from the saved module into the local session (I used the -Prefix parameter to avoid clobbering my local commands). No connection has been made to the remote computer yet, but I can see all the remote commands in <a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkID=113309\">Get-Command<\/a> and use tab completion when typing them at the command prompt. <\/p>\n<p>Let&#8217;s try to invoke one of the commands: <\/p>\n<pre>PS&gt; Get-RemoteProcess -Name w*host\nCreating a new session for implicit remoting of &quot;Get-Process&quot; command...\nHandles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName\n-------  ------    -----      ----- -----   ------     -- -----------\n    237       9    24372      35556   146     0.95   4344 wsmprovhost<\/pre>\n<p>The remote invocation worked. I didn&#8217;t even have to create a remote session &#8211; implicit remoting took care of that when I first attempted to use a remote command from the saved module. I&#8217;ve been prompted for the password, but all the other connection parameters (i.e. computer name, http proxy settings) were stored in the saved module. <\/p>\n<p>Thanks,<\/p>\n<p>Lukasz Anforowicz [MSFT]\n  <br \/>Windows PowerShell Developer\n  <br \/>Microsoft Corporation<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Read the previous post on implict remoting to learn how the Import-PSSession cmdlet makes it easier to work with remote commands by presenting them as if they were local commands. This user experience saves you the trouble of typing long Invoke-Command incantations to pass arguments to remote commands or to download remote help content. The [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[8],"class_list":["post-3241","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-remoting"],"acf":[],"blog_post_summary":"<p>Read the previous post on implict remoting to learn how the Import-PSSession cmdlet makes it easier to work with remote commands by presenting them as if they were local commands. This user experience saves you the trouble of typing long Invoke-Command incantations to pass arguments to remote commands or to download remote help content. The [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=3241"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3241\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=3241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=3241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=3241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}