diff --git a/demos/python/README.md b/demos/python/README.md new file mode 100644 index 0000000000..66ec4862d7 --- /dev/null +++ b/demos/python/README.md @@ -0,0 +1,8 @@ +## PowerShell/Python Interoperation Demo + +The "demo_script.ps1" file in this directory walks through a +demonstration of basic interoperation between PowerShell and Python +including how to use JSON to exchange structured objects between +Python and PowerShell. + +The other files in this directory are referenced by demo_script.ps1. diff --git a/demos/python/class1.ps1 b/demos/python/class1.ps1 new file mode 100644 index 0000000000..16b426a91d --- /dev/null +++ b/demos/python/class1.ps1 @@ -0,0 +1,11 @@ +# +# Wrap Python script in such a way to make it easy to +# consume from PowerShell +# +# The variable $PSScriptRoot points to the directory +# from which the script was executed. This allows +# picking up the Python script from the same directory +# + +& $PSScriptRoot/class1.py | ConvertFrom-JSON + diff --git a/demos/python/class1.py b/demos/python/class1.py new file mode 100755 index 0000000000..23209a9208 --- /dev/null +++ b/demos/python/class1.py @@ -0,0 +1,18 @@ +#!/usr/bin/python3 + +import json + +# Define a class with a method that returns JSON +class returnsjson: + def method1(this): + return json.dumps(['foo', + { + 'bar': ('baz', None, 1.0, 2), + 'buz': ('foo1', 'foo2', 'foo3') + }, + 'alpha', + 1,2,3]) + +c = returnsjson() +print (c.method1()) + diff --git a/demos/python/demo_script.ps1 b/demos/python/demo_script.ps1 new file mode 100644 index 0000000000..0544efe15e --- /dev/null +++ b/demos/python/demo_script.ps1 @@ -0,0 +1,61 @@ +# +# Demo simple interoperation between PowerShell and Python + +# Basic execution of a Python script fragment +python -c "print('Hi!')" + +# Capture output in a variable +$data = python -c "print('Hi!')" + +# And show the data +$data + +# Use in expressions +5 + (python -c "print(2 + 3)") + 7 + +# Create a Python script using a PowerShell here-string, no extension +@" +#!/usr/bin/python3 +print('Hi!') +"@ | out-file -encoding ascii hi + +# Make it executable +chmod +x hi + +# Run it - shows that PowerShell really is a shell +./hi + +# A more complex script that outputs JSON +cat class1.py + +# Run the script +./class1.py + +# Capture the data as structured objects (arrays and hashtables) +$data = ./class1.py | ConvertFrom-JSON + +# look at the first element of the returned array +$data[0] + +# Look at the second +$data[1] + +# Get a specific element from the data +$data[1].buz[1] + +# Finally wrap it all up so it looks like a simple PowerShell command +cat class1.ps1 + +# And run it, treating the output as structured data. +(./class1)[1].buz[1] + +# Finally a PowerShell script with in-line Python +cat inline_python.ps1 + +# and run it +./inline_python + + +#################################### +# cleanup +rm hi diff --git a/demos/python/inline_python.ps1 b/demos/python/inline_python.ps1 new file mode 100644 index 0000000000..c9026304c9 --- /dev/null +++ b/demos/python/inline_python.ps1 @@ -0,0 +1,16 @@ +# +# An example showing inline Python code in a PowerShell script +# + +"Hello from PowerShell!" + +# Inline Python code in a "here string" which allows for a multi-line script +python3 -c @" +print(' Hello from Python!') +print(' Python and PowerShell get along great!') +"@ + +# Back to PowerShell... +"Back to PowerShell." +"Bye now!" +