1

I am a noob, self-motivated programmer, and had been researching methods to use my Python script to run a Powershell file that will copy and image and place the image into Excel.

I've used the subprocess, call, and Popen commands in an attempt to call and run the Powershell program from the Python script, but none has worked.

Some of the examples I found only called different functions of a Powershell script, but when I tried those settings it didn't work for my program. All of the setup for my Powershell has been established so that it can run with my PC, and also runs well when launched independently from Python.

What I would like to ask is if I had, for example, a My_program.py file and a Moving_image.ps1 file. I want to use my .py file to run/execute my .ps1 file, while both programs are located in the same path (C:\Users\Scripts).

What line of code(s), imports, and other program setup's would I need in my Python file to simply run the independent .ps1 file from my Python script?

I don't need the Powershell script to return anything to the Python script. I would like for it to simply run the copy and paste the command I sent it.

Thank you. Any type of guidance that will lead to this program actually functioning properly will be most appreciated!

2

1 Answer 1

1

Here's what worked for me (testing on linux):

python script test.py

from subprocess import call
call(["/opt/microsoft/powershell/6.0.4/pwsh", "./test.ps1"])

powershell script test.ps1

Import-Module '/home/veefu/pwshmodules/testMod'
Write-Output "Hello from test.ps1"
Get-HelloWorld

testMod.psm1 module, stored at /home/veefu/pwshmodules/testMod

function Get-HelloWorld {
    Write-Output "Hello World (from Module)"
}

result when running the python script:

Hello from test.ps1
Hello World (from Module)

On windows you'll probably have to provide the complete path, C:\Windows\system32\MicrosoftPowerShell\1.0\powershell.exe and you may have to pass /file .\yourOtherScript.ps1 in the second argument to call

Sign up to request clarification or add additional context in comments.

14 Comments

Can you use subprocess.run?
I have tried the subprocess.run and the subprocess.call command but neither of the two seem to be fully functional. On one end I think Its not receiving any error on the python end because the shell comes back with no error. So after running the script the python script opens the powershell window but then it out puts a few red errors and closes pretty fast
The programs are running off a Windows based machine and I created two of the same programs that you listed in your example which were test.py and test.ps1.
subprocess.Popen([r"C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", "/file ./test.ps1"])
This doesn't show any error as stated above on the python shell after it runs so I assume that this one is working properly, but when it opens the powershell window and in the process of running the test.ps1 script, it says about 4 lines of error and closes pretty fast.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.