Calling a .ps1 script in Linux Powershell from a Flask route in Python

km457

I'm trying to invoke a .ps1 script in Powershell for Linux by passing a variable and triggering it in reaction to a web call to a Flask/Python API.

All the files are in the same directory in this example - Ideally the .ps1 script and other relevant files to the action it needs to take would be in another directory, but for testing, they're all in the main directory of my venv.

If I run the following code manually, via my_venv >> python script.py

# this is script.py
import subprocess

arg1 = 'xyz'
arg2 = 'abc'

subprocess.run(['pwsh', '.\example.ps1', arg1, arg2])

It will work properly. Powershell will run, and the actions in the script example.ps1 will execute. However, if I add the same Python code to a Flask app route so it can be triggered by an API request like so:

from flask import Flask, request
import subprocess
app = Flask(__name__)
app.debug = True

# example route
@app.route('/example/', methods=['GET'])
def example():

    var1 = request.args.get('var1')
    arg1 = var1
    arg2 = 'abc'

    subprocess.run(['pwsh', '.\example.ps1', arg1, arg2])

    return ('success', 200)

It doesn't do anything. Flask debugging gives me the error:

Exception: builtins.FileNotFoundError: [Errno 2] No such file or directory: 'pwsh'

Which makes me think it's not locating the binary for pwsh but I'm not clear on how to fix that in this situation. In Windows, you'd put the path the the powershell.exe executable in your command, but that's obviously not how it works here.

One note is the variable above - I've tried it without, just letting a value pass to var1 via GET and then ignoring it and hardcoding arg1 to test, makes no difference. The real code does need the variable.

Any obvious dumb thing I'm doing wrong here? This question doesn't seem to have been asked for Linux, although there are some similar Windows questions.

Maximilian Burszley

In Windows, it's usually a best-practice to fully path your executables and arguments if you're unsure of the environment variables. You can accomplish this in your example by using

subprocess.run(['/usr/bin/pwsh', '.\example.ps1', arg1, arg2])

to fully qualify the pwsh executable path in Linux.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From

Call PowerShell script PS1 from another PS1 script inside Powershell ISE

From Dev

PowerShell - Launch GUI from PowerShell script (.ps1) directly

From Dev

How to run powershell script from .ps1 file?

From Dev

Calling a ps1 from another ps1

From Dev

Calling PS1 script from other PS1 script failing after invoked for the 1st time in same Terminal instance, Why?

From Dev

Running a PowerShell Script .PS1

From Dev

Run Powershell script from anywhere in VScode (basically hotkey a .ps1 script)

From Dev

Starting .ps1 Script from PowerShell with Parameters and Credentials and getting output from it

From Dev

Linux Script Command With PS1 Change

From Dev

Starting .ps1 Script from PowerShell with Parameters and Credentials and getting output using variable

From Dev

PowerShell .ps1 script file is deleted when run from CMD

From Dev

Running cmd /c from a PowerShell ps1 script using Stop-Parsing

From Dev

Powershell - running a ps1 script on all ps1 files in a directory

From Dev

Best way to execute powershell script ".ps1"

From Dev

Calling ps on Linux from Java

From Dev

Calling Powershell script from CMD with paths as parameters

From Dev

Calling a PowerShell script from a batch file

From Dev

Calling NodeJs script from Powershell - Encoding problem

From Dev

Calling python script from VBA

From Dev

Calling a SQL script from python

From Dev

Calling a shell script from python

From Java

Calling Rscript from linux shell script

From Dev

calling python script from another script

From Dev

How do I create a bat file that executes powershell script without an external ps1 file?

From Dev

Running a self decrypting base64 powershelll script locally with powershell -file /path/to/ps1

From

Is there a way to make a PowerShell script work by double clicking a .ps1 file?

From Dev

Powershell ps1 script runs bat file but CMD screen closes after execution

From Dev

Why does this New-Item command work within powershell but not within a ps1 script?

From Dev

Azure Pipeline PowerShell Script Error "A path to a .ps1 file is required"

Related Related

  1. 1

    Call PowerShell script PS1 from another PS1 script inside Powershell ISE

  2. 2

    PowerShell - Launch GUI from PowerShell script (.ps1) directly

  3. 3

    How to run powershell script from .ps1 file?

  4. 4

    Calling a ps1 from another ps1

  5. 5

    Calling PS1 script from other PS1 script failing after invoked for the 1st time in same Terminal instance, Why?

  6. 6

    Running a PowerShell Script .PS1

  7. 7

    Run Powershell script from anywhere in VScode (basically hotkey a .ps1 script)

  8. 8

    Starting .ps1 Script from PowerShell with Parameters and Credentials and getting output from it

  9. 9

    Linux Script Command With PS1 Change

  10. 10

    Starting .ps1 Script from PowerShell with Parameters and Credentials and getting output using variable

  11. 11

    PowerShell .ps1 script file is deleted when run from CMD

  12. 12

    Running cmd /c from a PowerShell ps1 script using Stop-Parsing

  13. 13

    Powershell - running a ps1 script on all ps1 files in a directory

  14. 14

    Best way to execute powershell script ".ps1"

  15. 15

    Calling ps on Linux from Java

  16. 16

    Calling Powershell script from CMD with paths as parameters

  17. 17

    Calling a PowerShell script from a batch file

  18. 18

    Calling NodeJs script from Powershell - Encoding problem

  19. 19

    Calling python script from VBA

  20. 20

    Calling a SQL script from python

  21. 21

    Calling a shell script from python

  22. 22

    Calling Rscript from linux shell script

  23. 23

    calling python script from another script

  24. 24

    How do I create a bat file that executes powershell script without an external ps1 file?

  25. 25

    Running a self decrypting base64 powershelll script locally with powershell -file /path/to/ps1

  26. 26

    Is there a way to make a PowerShell script work by double clicking a .ps1 file?

  27. 27

    Powershell ps1 script runs bat file but CMD screen closes after execution

  28. 28

    Why does this New-Item command work within powershell but not within a ps1 script?

  29. 29

    Azure Pipeline PowerShell Script Error "A path to a .ps1 file is required"

HotTag

Archive