{"id":80825,"date":"2016-11-11T00:01:52","date_gmt":"2016-11-11T08:01:52","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=80825"},"modified":"2019-02-18T09:10:21","modified_gmt":"2019-02-18T16:10:21","slug":"open-source-powershell-part-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/open-source-powershell-part-2\/","title":{"rendered":"Open Source PowerShell \u2013 Part 2"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use PowerShell scripts within Python and Bash (and those within PowerShell).<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/> I am curious just how much I can use Open Source PowerShell within Linux and macOS.\u00a0 Is it an isolated environment, or can it interact with other shells?<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/> Thanks for your question.\u00a0 Our good friend, Honorary Scripting Guy, Sean Kearney, is here today to show you just how well it interacts.<\/p>\n<p>Let\u2019s consider something. Most shells, whether it is Bash or CMD.EXE, are just that, a shell. They normally can run some processes and send output to the console.<\/p>\n<p>In many cases, those same shells can call up other shells and launch others within those. If you\u2019re not careful, you\u2019ll have so many shells inside of shells that you\u2019ll have a whole \u201cC\u201d of them!<\/p>\n<p>Okay, that\u2019s officially my bad, nerdy pun for the week. I\u2019ll collect the tomatoes in a few minutes. Let\u2019s do something seriously interesting. Let\u2019s go through some shell work.<\/p>\n<p>First, we\u2019re going to start PowerShell on an Ubuntu box and run a small line of Python code within it.<\/p>\n<p style=\"padding-left: 60px\"><code>powershell\npython -c \"print('Hello Everybody this is PowerShell Polly\u2026RAWK!')\"<\/code><\/p>\n<p>The output is actually pretty simple and expected: a simple printed output to the terminal.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-111116.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-111116.png\" alt=\"Starting PowerShell on an Ubuntu box and running a small line of Python code within it\" width=\"619\" height=\"151\" class=\"alignnone size-full wp-image-80835\" \/><\/a><\/p>\n<p>First, what did this do? We ran a command within the Python shell, it interacted with the Linux terminal, and returned back to PowerShell.<\/p>\n<p>In a nutshell, PowerShell ran Python. We can also have the two systems seamlessly interact with each other.<\/p>\n<p>As well, just as we can capture the output of a console application in Windows by assigning the results to PowerShell, we can do the exact same thing in Open Source PowerShell.<\/p>\n<p style=\"padding-left: 60px\"><code>$ThisLittleObject= python -c \"print('Hello Everybody this is PowerShell Polly\u2026RAWK!')\"<\/code><\/p>\n<p>What this now does, even though the output is text from a Linux shell, is allow us to capture it as a <strong>[String]<\/strong> object, giving us the ability to examine and manipulate that data with PowerShell methods.<\/p>\n<p>This is big.<\/p>\n<p>We are now accessing Linux shell results from PowerShell. We can now use all the built-in methods, regular expressions, and even cmdlets like <strong>ConvertFrom-StringData<\/strong> to change them to even more powerful objects.<\/p>\n<p>But, here is the more critical piece to keep in mind. It\u2019s just about working in PowerShell only. In many Linux and macOS environments, perhaps the bulk of the work already exists as Bash scripts. Perhaps you might only need to call up PowerShell for something specific.<\/p>\n<p>Can you?<\/p>\n<p>Well, just as I can launch a Python script in Bash, I can do the exact same thing with PowerShell. Consider this simple example in which I launch a PowerShell script from Bash and allow the output to echo to the terminal as normal.<\/p>\n<p style=\"padding-left: 60px\"><code>powershell -file fooscript.ps1<\/code><\/p>\n<p>So, the neat part is that PowerShell is behaving like every other Linux shell. Thumbs up!<\/p>\n<p>This means that I should be able to capture the output into a Linux variable like every other Bash or Python script. That\u2019s right\u2026.<u>this works<\/u>!<\/p>\n<p style=\"padding-left: 60px\"><code>SomeThingISee=powershell -file fooscript.ps1<\/code><\/p>\n<p>Now, consider this bizarre statement. I\u2019m going to have Python launch a PowerShell script and then all <u>this<\/u> to be called from Bash.<\/p>\n<p style=\"padding-left: 60px\"><code>python -c \"import os; os.system('powershell -file fooscript.ps1')\"<\/code><\/p>\n<p>So, from a standpoint of Linux or macOS administrators, it\u2019s just a another shell they can leverage which integrates as well as any other shell.<\/p>\n<p>Pop around next time, and we show how to pass parameters between Open Source PowerShell and Linux \/ macOS shells.<\/p>\n<p>I invite you to follow the Scripting Guys on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to them at <a target=\"_blank\" href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow.<\/p>\n<p>Until then, always remember that with Great PowerShell comes Great Responsibility.<\/p>\n<p><strong>Sean Kearney\n<\/strong>Honorary Scripting Guy\nCloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use PowerShell scripts within Python and Bash (and those within PowerShell). I am curious just how much I can use Open Source PowerShell within Linux and macOS.\u00a0 Is it an isolated environment, or can it interact with other shells? Thanks for your question.\u00a0 Our good friend, Honorary Scripting Guy, Sean Kearney, is here today [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568,685,641],"tags":[56,154,45],"class_list":["post-80825","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-scripting-techniques","category-windows-powershell","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use PowerShell scripts within Python and Bash (and those within PowerShell). I am curious just how much I can use Open Source PowerShell within Linux and macOS.\u00a0 Is it an isolated environment, or can it interact with other shells? Thanks for your question.\u00a0 Our good friend, Honorary Scripting Guy, Sean Kearney, is here today [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=80825"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80825\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=80825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=80825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=80825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}