Open Source PowerShell – Part 2

Doctor Scripto

Summary: Use PowerShell scripts within Python and Bash (and those within PowerShell).

Hey, Scripting Guy! Question I am curious just how much I can use Open Source PowerShell within Linux and macOS.  Is it an isolated environment, or can it interact with other shells?

Hey, Scripting Guy! Answer Thanks for your question.  Our good friend, Honorary Scripting Guy, Sean Kearney, is here today to show you just how well it interacts.

Let’s 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.

In many cases, those same shells can call up other shells and launch others within those. If you’re not careful, you’ll have so many shells inside of shells that you’ll have a whole “C” of them!

Okay, that’s officially my bad, nerdy pun for the week. I’ll collect the tomatoes in a few minutes. Let’s do something seriously interesting. Let’s go through some shell work.

First, we’re going to start PowerShell on an Ubuntu box and run a small line of Python code within it.

powershell python -c "print('Hello Everybody this is PowerShell Polly…RAWK!')"

The output is actually pretty simple and expected: a simple printed output to the terminal.

Starting PowerShell on an Ubuntu box and running a small line of Python code within it

First, what did this do? We ran a command within the Python shell, it interacted with the Linux terminal, and returned back to PowerShell.

In a nutshell, PowerShell ran Python. We can also have the two systems seamlessly interact with each other.

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.

$ThisLittleObject= python -c "print('Hello Everybody this is PowerShell Polly…RAWK!')"

What this now does, even though the output is text from a Linux shell, is allow us to capture it as a [String] object, giving us the ability to examine and manipulate that data with PowerShell methods.

This is big.

We are now accessing Linux shell results from PowerShell. We can now use all the built-in methods, regular expressions, and even cmdlets like ConvertFrom-StringData to change them to even more powerful objects.

But, here is the more critical piece to keep in mind. It’s 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.

Can you?

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.

powershell -file fooscript.ps1

So, the neat part is that PowerShell is behaving like every other Linux shell. Thumbs up!

This means that I should be able to capture the output into a Linux variable like every other Bash or Python script. That’s right….this works!

SomeThingISee=powershell -file fooscript.ps1

Now, consider this bizarre statement. I’m going to have Python launch a PowerShell script and then all this to be called from Bash.

python -c "import os; os.system('powershell -file fooscript.ps1')"

So, from a standpoint of Linux or macOS administrators, it’s just a another shell they can leverage which integrates as well as any other shell.

Pop around next time, and we show how to pass parameters between Open Source PowerShell and Linux / macOS shells.

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow.

Until then, always remember that with Great PowerShell comes Great Responsibility.

Sean Kearney Honorary Scripting Guy Cloud and Datacenter Management MVP

0 comments

Discussion is closed.

Feedback usabilla icon