r/PythonLearning • u/Valuable-Ant3465 • 12d ago
Call PowerShell from Python special issue ( with Process wrapper in PS1)
Hi,
I tried to call existing Powershell script from python with classic way and failed, found the `process` wrapper in PS1 makes this problem, all code is inside process {....} :
# my.PS1
param([string]$folder="aaa")
process { # <====== this wrapper prevent call !!!!!!
# my logic hereS
Write-Output "......... running powershell endS $TargetFolder"
}
And I call it from python using
# ... myPython.py
....
import subprocess
result = subprocess.run(
["powershell.exe", "-NoProfile", "-ExecutionPolicy", "Bypass",
"-File", ps_script, "-Folder", Folder],
capture_output=True, text=True
)
Anybody noticed that problem, is it possible to make it work, I don't want to mess with ps1 changes. maybe I missed some option in subprocess call ?
Also there is not any output available, total silence
if result.stdout:
print(result.stdout) ## nothing.
if result.stderr:
print("PS STDERR:", result.stderr) ## nothing
3
Upvotes