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!

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
  • This Is all also being performed on a windows 10 base machine! – Pierry Louis Aug 12 '18 at 04:01
  • Possible duplicate of [Running powershell script within python script, how to make python print the powershell output while it is running](https://stackoverflow.com/questions/21944895/running-powershell-script-within-python-script-how-to-make-python-print-the-pow) – Dawid Zbiński Aug 12 '18 at 06:43

1 Answers1

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

veefu
  • 2,820
  • 1
  • 19
  • 29
  • Can you use `subprocess.run`? – lit Aug 12 '18 at 11:48
  • 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 – Pierry Louis Aug 12 '18 at 22:11
  • 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. – Pierry Louis Aug 12 '18 at 22:15
  • subprocess.Popen([r"C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", "/file ./test.ps1"]) – Pierry Louis Aug 12 '18 at 22:27
  • 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. – Pierry Louis Aug 12 '18 at 22:29
  • write-output "Hello from powershell" – Pierry Louis Aug 12 '18 at 22:30
  • are there any other type of program set up that I have to enable within the system in order to run the powershell script? – Pierry Louis Aug 12 '18 at 22:33
  • It might be the command-line is `-file` instead of `/file`. Check docs [here](https://learn.microsoft.com/en-us/powershell/scripting/core-powershell/console/powershell.exe-command-line-help?view=powershell-6) You could also try providing a complete path to the test.ps1 file; it might be that python isn't setting the current working directory for the powershell.exe process. – veefu Aug 13 '18 at 01:41
  • so I am now able to get the python script to call an external powershell program that can print a line of code on the PS window. but now going back to my script I want to continue to use python to send a run powershell command where powershell will send print commands to excel. I am receiving errors of where I am being told that my excel commands don't exist but when i run the program separately it works fine. So i think the issue is related to powershell library with excel – Pierry Louis Aug 13 '18 at 03:39
  • You should post a new question about the Excel problem. Include the minimum amount of code to reproduce the issue. – veefu Aug 13 '18 at 04:15
  • Thanks so much, I also ran some testing and found that the real problem is that when python runs to call the powershell script and runs it. The script actually partially runs now but runs into an error. It runs into an error because its not importing the "WASP" powershell import which is causing all of the commands being sent to excel that utilize the WASP library not to be recognized. So really the problem now is how can I import the powershell library/module "WASP" in my program when using python to call a powershell script. – Pierry Louis Aug 14 '18 at 00:15
  • I take it putting `Import-Module WASP` in your powershell script didn't work? I think it'd be helpful if you were able to capture the errors from your powershell script. Add a [`Start-Transcript` and `End-Transcript`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-6) to your ps1, using `-path` to write to a file somewhere. Then run your test and analyze the transcript for clues. Perhaps python isn't passing along all the environment variables, like PSModulePath. The transcript should give you a path of investigation. – veefu Aug 14 '18 at 00:31
  • no unfortunately that didnt work either, I tried creating a profile exe and putting in a Add-PSSnapin WASP command but that still isn't allowing my program to run with the WASP import but it allows me to run WASP commands on boot when i open powershell. – Pierry Louis Aug 16 '18 at 20:35
  • I expanded my test to include a simple module. Placing the line `Import-Module /complete/path/to/moduleDirectory` allowed the powershell script to find and run the functions in the module. Can you try importing the WASP module using the complete path to the module? – veefu Aug 17 '18 at 09:38