How to call and pass arguments to a PowerShell .ps1 file from Python?

Python Code calling PowerShell file with arguments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import subprocess
 
def callps1():
 powerShellPath = r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe'
 powerShellCmd = "C:\PowerShellSample.ps1"
 
 p = subprocess.Popen([powerShellPath, '-ExecutionPolicy', 'Unrestricted', powerShellCmd, 'HELLO', 'WORLD']
 , stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 output, error = p.communicate()
 rc = p.returncode
 print "Return code given to Python script is: " + str(rc)
 print "\n\nstdout:\n\n" + str(output)
 print "\n\nstderr: " + str(error)
 
#Test
callps1()

PowerShell file, printing the arguments passed

1
2
3
4
5
6
7
param(
    [string]$arg1,
    [string]$arg2
)
 
Write-Output $arg1
Write-Output $arg2

Output

1
2
3
4
5
stdout:
HELLO
WORLD
stderr: 
>>>

A personal technical diary… To the point… Precise

Previous

Great free tools for image editing online!

Next

What is Cloud?

7 Comments

  1. ig

    I like it whenever people come together and share views.
    Great blog, continue the good work!

  2. Hey there! Quick question that’s completely off topic.
    Do you know how to make your site mobile friendly? My weblog looks weird when viewing from my apple iphone.

    I’m trying to find a theme or plugin that might be able to correct this issue.

    If you have any suggestions, please share. Appreciate it!

    • Techno

      Thank you for your question, you will have to experiment a little with varioud themes available in WordPress. Once you have a theme if you click on the appearance section there on the bottom right of the left navigation you will see option to toggle between PC, Tablet & mobile view there you can test and see if the theme you are having works on all 3

  3. After study some of the content in your web site now, and i also really much like your technique of blogging. I bookmarked it to my bookmark web site list and will also be checking back soon. Pls take a look at my internet site as well and told me what you believe.

    • Overall after seeing your site softbizscripts.com I think functionally it is nice. How ever I think you can do some research on color combination part. Usually a good way to go about it is to select 2 to 3 colors that compliment each other and are easier to read for the human eye and then use those colors throughout in your web site. There are some web sites that help you choose good color combinations for your web site. You actually inspired me to write my next post. Please see here how to choose color combinations for a web site.

      https://technovechno.com/choose-color-combinations-website/

  4. I think this internet site contains very fantastic written subject matter blog posts.

Leave a Reply

Home | Contact Us | Terms and Conditions | Theme By Anders Norén