Python Forum
Cant get powershell script to run with subprocess
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cant get powershell script to run with subprocess
#1
hi all,

i can run up a powershell command with subprocess fine but now im trying to get subprocess working with my powershell script but its not passing the variables

heres my py script
import subprocess

un = "username"
op = "oldpassword"
cnp = "newpassword"
dom = "robo84"

subprocess.run(f'powershell.exe C:\\python\\test.ps1' , shell=True)
ps1 script
write-host $un
write-host $op 
write-host $cnp 
write-host $dom
but when i run in cmd, it runs with no errors but i get 4 blank lines

thanks,
rob
Reply
#2
Do not use shell=True.
Try this example:
import os
import subprocess
from pathlib import Path


# My Windows 11 installation is broken
# The Path is missing in my environment variables
# I guess you could run powershell.exe directly without entering the whole Path
system_root = os.environ["SystemRoot"]
powershell_exe = Path(
    r"%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe".replace("%SystemRoot%", system_root)
)


# A random script copied from internet
# source: https://www.mariotti.de/powershell-dialog/
ps_script = """
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'My Title'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please select a value:'
$form.Controls.Add($label)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 80
[void] $listBox.Items.Add('Value1')
[void] $listBox.Items.Add('Value2')
[void] $listBox.Items.Add('Value3')
[void] $listBox.Items.Add('Value4')
[void] $listBox.Items.Add('Value5')
[void] $listBox.Items.Add('Value6')
[void] $listBox.Items.Add('Value7')
$form.Controls.Add($listBox)
$form.Topmost = $true
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $listBox.SelectedItem
    $x
)
"""

# here the process is called.
# it blocks the code until the process is done
# not using shell=True, which is unsafe
# input is stdin of the provess. If you choose utf8 as encoding, then you can use strings
# if encoding is not given, input expects bytes
print("Running process with Window")
proc = subprocess.run([powershell_exe], input=ps_script, encoding="utf8")


# if you want to hide the powershell window:
print("Running process without Window")
creationflags = subprocess.CREATE_NO_WINDOW
proc = subprocess.run([powershell_exe], input=ps_script, encoding="utf8", creationflags=creationflags)
I have no clue how you get Information from the powershell script, that you can use it in python after the process finished.
But the menu seems to work.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
ok solved

ps1 script
param (
[string]$un,
[string]$op,
[string]$cnp,
[string]$dom
)

write-host $un
write-host $op
write-host $cnp
write-host $dom
py script
import subprocess

un = "firstname.lastname"
op = "oldpassword"
cnp = "newpassword"
dom = "domain"

subprocess.run(f'powershell.exe C:\\python\\test.ps1 {un} {op} {cnp} {dom}', shell=True)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Show powershell errors in flask-wtf web form robertkwild 12 322 Yesterday, 11:45 AM
Last Post: robertkwild
  Running powershell command in flask wtf form robertkwild 10 318 Jun-27-2024, 09:49 AM
Last Post: robertkwild
  Passing web form to powershell robertkwild 1 277 Jun-14-2024, 10:16 AM
Last Post: Akshiya
  using PowerShell from Python script for mounting shares tester_V 8 848 Mar-12-2024, 06:26 PM
Last Post: tester_V
  PowerShell & Python deep_logic 2 848 Jun-06-2023, 06:34 AM
Last Post: buran
  Running script with subprocess in another directory paul18fr 1 4,554 Jan-20-2023, 02:33 PM
Last Post: paul18fr
  How to write a part of powershell command as a variable? ilknurg 2 1,235 Jul-26-2022, 11:31 AM
Last Post: ilknurg
Photo Windows 10 PowerShell doesn't work Amy 3 4,057 Apr-27-2021, 01:33 PM
Last Post: jefsummers
  how to set focus when running a script using subprocess Malt 0 3,250 Sep-26-2019, 08:07 AM
Last Post: Malt
  Using Subprocess.Popen to start another python script running in background on Window johnb546 0 13,819 Jun-01-2018, 01:57 PM
Last Post: johnb546

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020