PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms


HTML | 216 lines | 208 code | 8 blank | 0 comment | 0 complexity | 3a8045628ac071c6974dbe8552afd032 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>
  2. <head>
  3. <link rel="stylesheet" type="text/css" href="../Samples.css">
  4. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  5. <title>PowerShell from IronPython</title>
  6. <style type="text/css">
  7. .style5 {
  8. margin-left: 40px;
  9. }
  10. .style3 {
  11. font-weight: normal;
  12. }
  13. </style>
  14. </head>
  15. <body class="body">
  16. <hr>
  17. <p class="CopyrightText">Information in this document is subject to change without notice. The example companies, organizations,
  18. products, people, and events depicted herein are fictitious. No association with any real company, organization, product,
  19. person or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of
  20. the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced
  21. into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or
  22. otherwise), or for any purpose, without the express written permission of Microsoft Corporation.</p>
  23. <p class="CopyrightText">&nbsp;</p>
  24. <p class="CopyrightText">Microsoft may have patents, patent applications, trademarked, copyrights, or other intellectual
  25. property rights covering subject matter in this document. Except as expressly provided in any written license agreement
  26. from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights,
  27. or other intellectual property.</p>
  28. <p class="CopyrightText">&nbsp;</p>
  29. <div class="Section1">
  30. <p class="CopyrightText">Š Microsoft Corporation. All rights reserved.</p>
  31. <p class="CopyrightText">&nbsp;</p>
  32. <p class="CopyrightText">Microsoft, MS-DOS, MS, Windows, Windows NT, MSDN, Active Directory, BizTalk, SQL Server, SharePoint,
  33. Outlook, PowerPoint, FrontPage, Visual Basic, Visual C++, Visual J++, Visual InterDev, Visual SourceSafe, Visual C#,
  34. Visual J#,&nbsp; and Visual Studio are either registered trademarks or trademarks of Microsoft Corporation in the
  35. U.S.A. and/or other countries.</p>
  36. <p class="CopyrightText">&nbsp;</p>
  37. <p class="CopyrightText">Other product and company names herein may be the trademarks of their respective owners.</p>
  38. </div>
  39. <hr>
  40. <p class="body">&nbsp;</p>
  41. <p class="CopyrightText">This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  42. copy of the license can be found in the License.html file at the root of this distribution. If
  43. you cannot locate the Apache License, Version 2.0, please send an email to
  44. By using this source code in any fashion, you are agreeing to be bound
  45. by the terms of the Apache License, Version 2.0.</p>
  46. <p class="CopyrightText">&nbsp;</p>
  47. <p class="body">&nbsp;</p>
  48. <p class="title2">PowerShell from IronPython</p>
  49. <p class="body">A brief example which utilizes managed Windows PowerShell APIs to run shell commands directly from IronPython.</p>
  50. <p class="body">&nbsp;</p>
  51. <p></p>
  52. <p class="contentsheading">Overview</p>
  53. <p class="Body">PowerShell (PS) is a new command-line shell for Windows which provides some pretty dramatic improvements over
  54. its predecessor - the DOS prompt. The most important new feature PS provides as far as IronPython is concerned
  55. is a set of managed interfaces accessible via the Common Language Runtime. We
  56. can tap directly into these from IronPython.</p><p class="Body">Utilizing PS from IronPython offers quite a bit
  57. more to developers than the standard Python modules used to run commands, manipulate the filesystem, etc. This sample
  58. includes two Python scripts, each of which are designed to determine if your host meets some of the minimum system requirements to
  59. play the game &quot;Age of Empires III Trial Edition&quot;. What sets these two scripts apart is that the first script,, leverages a helper module,, to obtain
  60. PS functionality in a very useful and
  61. object-oriented way. The second script,, uses PS commands directly to
  62. achieve the same results.</p><p class="Body">Note: the scripts distributed with this sample collect various performance
  63. and runtime info about your PC and emit this to the console for your own personal use.&nbsp; None of this information is
  64. transmitted to Microsoft.</p>
  65. <p class="contentsheading">Prerequisites</p>
  66. <p class="body">The prerequisites to successfully run the PowerShell from IronPython demo are:</p>
  67. <ul>
  68. <li class="normal">IronPython 2.6 distribution
  69. <ul>
  70. <li>If you are new to IronPython, please go over the tutorial
  71. that comes with this distribution</li>
  72. <li>Download from
  73. <a href="">here</a></li>
  74. </ul>
  75. </li>
  76. <li>
  77. <font face="Verdana" style="font-size: 11pt">Windows PowerShell 2.0</font><ul>
  78. <li>
  79. <font face="Verdana" style="font-size: 11pt">Needed to access the managed PS APIs</font></li>
  80. <li>
  81. <font face="Verdana" style="font-size: 11pt">Download from
  82. <a href="">here</a></font></li>
  83. </ul>
  84. </li>
  85. <li>
  86. <font face="Verdana" style="font-size: 11pt">Age of Empires III Trial Edition (<b>Optional</b>)</font><ul>
  87. <li><font face="Verdana" style="font-size: 11pt">Run this while executing the minsysreq* scripts distributed with this sample to get runtime info about Age
  88. of Empires III</font></li><li><font face="Verdana" style="font-size: 11pt">This step is entirely optional. The
  89. sample scripts will still run without installing Age of Empires III!</font></li><li>
  90. <font face="Verdana" style="font-size: 11pt">Download from
  91. <a href="">
  92. here</a></font></li>
  93. </ul>
  94. </li>
  95. </ul>
  96. <p class="contentsheading">Getting Started<br>
  97. </p>
  98. <p class="body">Please make sure the sample folder <i>IPPowerShell </i>(where this readme.htm is located)<i> </i>
  99. contains at least the following:</p>
  100. <blockquote>
  101. <blockquote>
  102. <p class="body">.\</p><p class="body">.\</p><p class="body">.\<br>
  103. </p>
  104. </blockquote>
  105. </blockquote>
  106. <p class="body">HINT: if the folder containing the IronPython distribution
  107. is not already in your PATH environment variable, please add it from the command
  108. prompt you'll be working in.&nbsp; By doing this you will be able to run the &quot;ipy&quot;
  109. command from any arbitrary directory: </p>
  110. <p class="style5"><strong>s</strong><span class="style3">e<span class="typedexample"><strong>t
  111. PATH=%PATH%;%ProgramFiles%\IronPython 2.6</strong></span></span></p>
  112. <p class="contentsheading">Using from an Interactive IronPython Session</p>
  113. <p class="body">From the command prompt, navigate to the <i>IPPowerShell</i> directory and type the following command: </p>
  114. <p class="typedexample">&gt; ipy.exe -i</p><p class="typedexample">&nbsp;</p>
  115. <p class="body">This starts an interactive IronPython session and imports quite a few modules,
  116. functions, objects, etc. into the global namespace. Most importantly you should find a &quot;shell&quot; object. &quot;shell&quot;
  117. is an instance of the Shell class (defined within which in turn is a wrapper for the standard PS
  118. cmdlets and aliases. You should take special note that the conversion from PS to IronPython involved the
  119. following rules with respect to Shell's methods:</p><ul>
  120. <li>
  121. <p class="body">for every PS cmdlet, there is a corresponding Shell method where the name is completely
  122. lowercase with &quot;-&quot;s translated into &quot;_&quot;s.&nbsp; E.g., the PS command &quot;<b>Get-Process</b>&quot; becomes the Shell method
  123. &quot;<b>shell.get_process()</b>&quot;</p></li>
  124. <li>
  125. <p class="body">for most simple PS aliases, there is a corresponding Shell method where the name is
  126. completely lowercase with &quot;-&quot;s translated into &quot;_&quot;s.&nbsp; E.g., the PS alias &quot;<b>ls</b>&quot; becomes the Shell
  127. method &quot;<b></b>&quot;. Aliases that do not directly map into a corresponding
  128. PS cmdlet (such as &quot;more&quot;) have no Shell method mapping.</p></li>
  129. <li>
  130. <p class="body">if a PS cmdlet accepts (non-parameter) arguments, they are supplied directly to the Shell
  131. method in the form of a single string parameter. E.g., the PS command &quot;<b>Write-Error &quot;Some error message
  132. here...&quot;</b>&quot; would become &quot;<b>shell.write_error(&quot;Some error message here...&quot;)</b>&quot;</p></li>
  133. <li>
  134. <p class="body">cmdlet parameters are mapped as keyword parameters of Shell methods.&nbsp; E.g., the PS
  135. command &quot;<b>Get-Date -DisplayHint date</b>&quot; becomes the Shell method &quot;<b>shell.get_date(DisplayHint='date')</b>&quot; </p></li>
  136. <li>
  137. <p class="body">the return value from one cmdlet can be piped in as the input to another cmdlet. To do this, one just needs to invoke the 2nd cmdlet method on the return value of the first cmdlet.&nbsp; E.g., the
  138. PS command &quot;<b>Get-Process ipy | ConvertTo-Html</b>&quot; becomes &quot;<b>shell.get_process(&quot;ipy&quot;).convertto_html()</b>&quot;</p></li>
  139. <li>
  140. <p class="body">properties of the return value object from a PS cmdlet are accessible as Python object attributes.&nbsp;
  141. E.g., the PS command &quot;<b>Get-Process ipy | Select-Object -First 1 -Property WS</b>&quot; becomes &quot;<b>shell.get_process(&quot;ipy&quot;)[0].WS</b>&quot;&nbsp;
  142. </p></li>
  143. </ul>
  144. <p class="body">&nbsp;</p>
  145. <table border="0" width="100%" height="36" bgcolor="#E6E6E6" cellpadding="15" cellspacing="1">
  146. <tr>
  147. <td height="36" width="523">
  148. &gt;&gt;&gt; from powershell import *<br>
  149. &gt;&gt;&gt; shell.dir()<br>
  150. Directory: Microsoft.PowerShell.Core\FileSystem::E:\Ip\IronPython\Samples\IPPowerShell<br>
  151. <br>
  152. <br>
  153. Mode LastWriteTime Length Name<br>
  154. ---- ------------- ------ ----<br>
  155. -a--- 9/7/2006 3:38 PM 3602<br>
  156. -a--- 9/11/2006 12:54 PM 3206<br>
  157. -a--- 9/11/2006 12:21 PM 6261<br>
  158. -a--- 9/11/2006 1:06 PM 11567 readme.htm<br>
  159. &gt;&gt;&gt;<br>
  160. &gt;&gt;&gt;<br>
  161. &gt;&gt;&gt; shell.dir().sort(&quot;LastWriteTime&quot;, Descending=True).select_object(&quot;Name&quot;)<br>
  162. Name<br>
  163. ----<br>
  164. readme.htm<br>
  168. &gt;&gt;&gt;</td>
  169. </tr>
  170. </table>
  171. <p class="contentsheading">Checking Minimum System Requirements for AOE III Trial
  172. Edition</p>
  173. <p class="body">Included with this sample are two scripts designed to automatically determine if your system is capable
  174. of playing Age of Empires III Trial Edition by using PS API(s) from IronPython. The first script,
  175., uses the nice PS wrapper module, while uses the managed PS assembly directly
  176. to achieve the same goal. Furthermore, both scripts will emit some runtime information such as memory usage if AOE III
  177. is currently running. This serves as a nice example because the standard Python modules provide little support for
  178. obtaining this type of information.</p><p class="body">To run these scripts it is necessary to issue the following
  179. commands from the <i>IPPowerShell</i> directory: &quot;<b>ipy.exe</b>&quot; and &quot;<b>ipy.exe</b>&quot;. The output for both of
  180. these scripts should be identical and similar to the following:</p><p class="body">&nbsp;</p>
  181. <table border="0" width="100%" height="36" bgcolor="#E6E6E6" cellpadding="15" cellspacing="1">
  182. <tr>
  183. <td height="36" width="523">
  184. E:\Ip\IronPython\Samples\IPPowerShell&gt;ipy<br>
  185. This program will determine if your PC meets some of the<br>
  186. minimum system requires to play the game, Age of Empires III Trial.<br>
  187. <br>
  188. Your CPU is fast enough (2.211Ghz)!<br>
  189. <br>
  190. 256 MB is enough video memory!<br>
  191. <br>
  192. Have a nice day!</td>
  193. </tr>
  194. </table>
  195. <p class="body">&nbsp;</p><p class="body">If you are
  196. already running AOE III, you'd also see the following extra output:</p>
  197. <table border="0" width="100%" height="36" bgcolor="#E6E6E6" cellpadding="15" cellspacing="1">
  198. <tr>
  199. <td height="36" width="523">
  200. It appears as if you're currently running Age of Empires III Trial !<br>
  201. The game is using 77 megs of RAM and has a process ID of 3956.</td>
  202. </tr>
  203. </table>
  204. <p class="body">&nbsp;</p><p class="body"><font size="4">Please explore the contents of both scripts in the <i>IPPowerShell</i>
  205. directory to assist you in using PowerShell from your own IronPython scripts and modules! </font></p><p class="body">&nbsp;</p>
  206. <p class="body">This concludes our tutorial. Have fun playing with PowerShell from IronPython!</p>
  207. </body>
  208. </html>