<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://powershell.com/cs/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Dreaming in PowerShell : OpenWSMan, DCOM, Remoting, RPC</title><link>http://powershell.com/cs/blogs/tobias/archive/tags/OpenWSMan/DCOM/Remoting/RPC/default.aspx</link><description>Tags: OpenWSMan, DCOM, Remoting, RPC</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 (Build: 30929.2835)</generator><item><title>PowerShell V2 Remoting - a first glimpse</title><link>http://powershell.com/cs/blogs/tobias/archive/2008/11/06/powershell-v2-remoting-overview.aspx</link><pubDate>Thu, 06 Nov 2008 18:29:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:172</guid><dc:creator>Tobias Weltner</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://powershell.com/cs/blogs/tobias/rsscomments.aspx?PostID=172</wfw:commentRss><comments>http://powershell.com/cs/blogs/tobias/archive/2008/11/06/powershell-v2-remoting-overview.aspx#comments</comments><description>
&lt;p&gt;


&lt;/p&gt;
&lt;p&gt;With PowerShell V2, you can do more with less, and remoting is among one of the most important innovations. It actually integrates tightly with other features such as jobs, security and throttling, but today, I want to focus on Remoting only and give you some inspiration and ideas.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;img src="http://powershell.com/cs/cfs-file.ashx/__key/CommunityServer.Components.UserFiles/00.00.00.21.03.Forums/teched11.jpg" alt="" /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;span style="font-size:xx-small;"&gt;M&lt;span style="font-size:x-small;"&gt;icrosoft PowerShell MVPs Tobias, Dmitry, MOW at the Ask-the-Experts Booth at TechEd EMEA in Barcelona&lt;/span&gt;&lt;/span&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;span style="font-size:x-small;"&gt;Note&lt;/span&gt;: This blog series is based entirely on notes I hastily took while listening to Jeffrey Snovers public presentations in Barcelona.&amp;nbsp;I did not verify the code bits I published here (because then I would have had to use NDA bits). Don&amp;#39;t necessarily expect them to run flawlessly or even being complete. They do give you a great idea of what Remoting is and how it works. Feel free to comment and correct anything you discover. In December with the CTP3 publicly available, we then can all play with the real bits.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Back to the story. &lt;strong&gt;Here&amp;#39;s the mission:&lt;/strong&gt; PowerShell scripts should do more with less. Specifically, they should run locally or remotely, synchroneously or asynchroneously in the background, against single or multiple machines, using wide ranges of authentication and protocols.&lt;/p&gt;
&lt;h4 class="style1"&gt;Please welcome: PSSession!&lt;/h4&gt;
&lt;p&gt;The mother of all remoting is something called a &lt;strong&gt;&amp;quot;PSSession&amp;quot;, which is an environment where PowerShell can exist&lt;/strong&gt;. You do not necessarily need to care about PSSessions as PowerShell creates them automatically if you don&amp;#39;t. Let&amp;#39;s start with some V1-compatible local code:&lt;/p&gt;
&lt;div class="listing"&gt;
&lt;pre&gt;&lt;span class="var"&gt;$script&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; {&lt;span class="verbnoun"&gt;get-process&lt;/span&gt; p&lt;span class="op"&gt;*&lt;/span&gt;}&lt;br /&gt;&lt;span class="verbnoun"&gt;Invoke-Command&lt;/span&gt; &lt;span class="var"&gt;$script&lt;/span&gt; &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Here, the variable $script contains an executable piece of code (a so called script block) which lists all processes starting with &amp;quot;p&amp;quot;. So, where is the magic?&lt;/p&gt;
&lt;h4 class="style1"&gt;Executing Stuff Remotely&lt;/h4&gt;
&lt;p&gt;There is none. Not yet. The magic comes with the new &lt;strong&gt;-ComputerName&lt;/strong&gt; parameter that was added to many cmdlets. With just a few changes, the same code gets executed on a remote machine instead of your local box:&lt;/p&gt;
&lt;div class="listing"&gt;
&lt;pre&gt;&lt;span class="var"&gt;$cred&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="verbnoun"&gt;Get-Credential&lt;/span&gt; Administrator&lt;br /&gt;&lt;span class="verbnoun"&gt;Invoke-Command&lt;/span&gt; &lt;span class="modifier"&gt;-ComputerName&lt;/span&gt; &lt;span class="string"&gt;&amp;#39;testserver1&amp;#39;&lt;/span&gt; &lt;span class="modifier"&gt;-Cred&lt;/span&gt; &lt;span class="var"&gt;$cred&lt;/span&gt; &lt;span class="modifier"&gt;-Auth&lt;/span&gt; Basic &lt;span class="var"&gt;$script&lt;/span&gt; &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;$cred takes your credentials, and then &lt;strong&gt;Invoke-Command&lt;/strong&gt; invokes the script code on the machine you specified using the credentials you submitted. &lt;/p&gt;
&lt;p&gt;In this last example, PowerShell has automagically created a PSSession on the remote computer. Your PowerShell then connected to the remote session and executed your code. You can almost &amp;quot;feel&amp;quot; PowerShell create the remote PSSession because there is an initial lag when you start executing code remotely - which is precisely when the remote Session is created.&lt;/p&gt;
&lt;h4 class="style1"&gt;Preserving Sessions&lt;/h4&gt;
&lt;p&gt;This lag is good because it reminds you that you can optimize things and provide a remote session yourself, thus keeping the remote session around so next time you need to execute something on that machine, it goes a lot quicker.&lt;/p&gt;
&lt;div class="listing"&gt;
&lt;pre&gt;&lt;span class="comment"&gt;# Create a new Remote Session:&lt;/span&gt;&lt;br /&gt;&lt;span class="var"&gt;$s&lt;/span&gt; &lt;span class="op"&gt;=&lt;/span&gt; &lt;span class="verbnoun"&gt;New-PSSession&lt;/span&gt; &lt;span class="modifier"&gt;-computername&lt;/span&gt; &lt;span class="string"&gt;&amp;#39;Testserver&amp;#39;&lt;/span&gt; &lt;span class="modifier"&gt;-Cred&lt;/span&gt; &lt;span class="var"&gt;$cred&lt;/span&gt; &lt;span class="modifier"&gt;-Auth&lt;/span&gt; Basic&lt;br /&gt;&lt;br /&gt;&lt;span class="comment"&gt;# Use the session as often as needed to execute code on that machine:&lt;/span&gt;&lt;br /&gt;icm &lt;span class="var"&gt;$s&lt;/span&gt; &lt;span class="var"&gt;$script&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;quot;Icm&amp;quot; is the alias for &lt;strong&gt;Invoke-Command&lt;/strong&gt;, and this Cmdlet has two positional parameters: a session and a script block. So in this example, I first manually create my session using &lt;strong&gt;New-PSSession&lt;/strong&gt; and then, since I have a handle on that session, can reuse it throughout all following tasks.&lt;/p&gt;
&lt;h4 class="style1"&gt;Executing PS-Scripts Remotely&lt;/h4&gt;
&lt;p&gt;A slight variant executes entire PowerShell Scripts remotely. All you do is exchange the script block with the &lt;strong&gt;-filename&lt;/strong&gt; parameter and specify a PS1 script you have access to. PowerShell then reads that file, converts its content into a script block and runs your file remotely - no need to copy the script to the remote machine.&lt;/p&gt;
&lt;p&gt;Since &lt;strong&gt;-Computername&lt;/strong&gt; really can take an array of names (thus enabling you to run against hundreds or thousands of systems simultaneously), and since the command transmits the script you want to run to the target machines automatically, this truly is a bright new wonder world for distributed automation. By the way: To list all Cmdlets that actually have a -ComputerName parameter, do this:&lt;/p&gt;
&lt;div class="listing"&gt;
&lt;pre&gt;&lt;span class="verbnoun"&gt;Get-Help&lt;/span&gt; &lt;span class="op"&gt;*&lt;/span&gt; &lt;span class="modifier"&gt;-Parameter&lt;/span&gt; ComputerName &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Another remarkable tech fact is this: the remoting uses &lt;strong&gt;WS-Man&lt;/strong&gt; which in turn does not depend on old-fashioned RPC/DCOM but instead flexibly works over a variety of protocols like HTTPS/HTTP on port 80, traversing firewalls and other network barriers. Thus you can manage even tricky locked down DMZs this way (which immediately raises tons of security-related questions that will be addressed in a different blog post, rest assured the PS team invested a lot of thought into that as well):&lt;/p&gt;
&lt;div class="listing"&gt;
&lt;pre&gt;icm &lt;span class="modifier"&gt;-Computername&lt;/span&gt; &lt;span class="string"&gt;&amp;#39;server1&amp;#39;&lt;/span&gt;,&lt;span class="string"&gt;&amp;#39;server2&amp;#39;&lt;/span&gt;,&lt;span class="string"&gt;&amp;#39;server7&amp;#39;&lt;/span&gt; &lt;span class="modifier"&gt;-filepath&lt;/span&gt; .\&lt;span class="namespace"&gt;manageserver.ps1&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h4 class="style1"&gt;&amp;quot;Remote Desktop for Consoles&amp;quot;&lt;/h4&gt;
&lt;p&gt;While &lt;strong&gt;Invoke-Command&lt;/strong&gt; runs single commands against any PSSession, local or remote, you can also turn your entire PowerShell console into a remote session. It&amp;#39;s really a bit like &lt;strong&gt;Remote Desktop for Consoles&lt;/strong&gt;. &lt;/p&gt;
&lt;div class="listing"&gt;
&lt;pre&gt;&lt;span class="verbnoun"&gt;Enter-PSSession&lt;/span&gt; &lt;span class="modifier"&gt;-computername&lt;/span&gt; &lt;span class="verbnoun"&gt;den-core&lt;/span&gt;&lt;span class="op"&gt;-&lt;/span&gt;01&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This will create a new remote Session on the computer you specified and then redirect all console I/O. Your console is still on your computer, of course, but what you see and do is actually happening on the remote system. Of course, with this interactive approach you can connect only to one remote system at a&amp;nbsp; time.&lt;/p&gt;
&lt;p&gt;Once you are done, don&amp;#39;t forget to clean up behind you:&lt;/p&gt;
&lt;div class="listing"&gt;
&lt;pre&gt;&lt;span class="verbnoun"&gt;Get-PSSession&lt;/span&gt; | &lt;span class="verbnoun"&gt;Remove-PSSession&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h4 class="style1"&gt;Mobile Object Model&lt;/h4&gt;
&lt;p&gt;One important aspect of remoting deals with the object nature of PowerShell. As you probably know, PowerShell works with rich objects. However, rich objects cannot be beamed to another universe. The technology simply isn&amp;#39;t there, yet. So whenever you connect to a remote system, you do not get back the objects you expect. They still look and feel like objects but in reality, they are &amp;quot;deserialized&amp;quot; copies. Essentially, what that means is: &lt;strong&gt;they are read-only. No writable properties, no methods&lt;/strong&gt;. You simply get back a &amp;quot;ghost&amp;quot; representation of the actual live remote object. Microsoft calls this Mobile Object Model.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;In reality, the deserialized object does have writable properties but they are worthless because the deserialized object is no longer connected to the original object. Changing properties would only change your local copy of the object but would not do anything to the real object. It is a bit like ADSI objects without SetInfo(), or generally put, there is no way to play back the changes made to a deserialized object. &lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This sounds worse than it is because those read-only copies work fine for sorting, filtering and presenting data. You just can&amp;#39;t change things. If you wanted that, you would simply have to avoid to beam the objects to your local machine but instead do all code on the remote system.&lt;/p&gt;
&lt;h4 class="style1"&gt;Managing Heterogeneous Worlds (aka &amp;quot;what about Unix?&amp;quot;)&lt;/h4&gt;
&lt;p&gt;Finally, since Remoting uses a service called WS-Man, and since there is an open implementation available called &lt;strong&gt;OpenWSMan&lt;/strong&gt;, PowerShells&amp;#39; remoting architecture principally also supports non-Windows systems like Unix and Linux. More on that later.&lt;/p&gt;
&lt;p&gt;Let the PowerShell be with you!&lt;/p&gt;
&lt;p&gt;-Tobias&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="http://www.idera.com/powershellplus"&gt;PowerShellPlus&lt;/a&gt; &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://powershell.com/cs/aggbug.aspx?PostID=172" width="1" height="1"&gt;</description><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/WS-man/default.aspx">WS-man</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/OpenWSMan/default.aspx">OpenWSMan</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/Remoting/default.aspx">Remoting</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/V2/default.aspx">V2</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/Remove-PSSession/default.aspx">Remove-PSSession</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/Enter-PSSession/default.aspx">Enter-PSSession</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/RPC/default.aspx">RPC</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/DMZ/default.aspx">DMZ</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/New-PSSession/default.aspx">New-PSSession</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/Get-PSSession/default.aspx">Get-PSSession</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/PSSession/default.aspx">PSSession</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/DCOM/default.aspx">DCOM</category><category domain="http://powershell.com/cs/blogs/tobias/archive/tags/Unix/default.aspx">Unix</category></item></channel></rss>