1

I'm trying to run a PowerShell script as administrator from python

As I cannot achieve this directly I tried running a batch file that run a PowerShell script

python run bat file

p = Popen([batFile, param1, param2, param3])

bat file run powershell

set param1=%1
set param2=%2
set param3=%3

PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dp0test.ps1"" ""%param1%"" ""%param2%"" ""%param3%""' -Verb RunAs}"

powershell code

param
(
    [string]$param1,
    [string]$param2,
    [string]$param13
)

Write-Output $param1
Write-Output $param2
Write-Output $param3


Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');

My problem is that actually seems like the code is executed but the PowerShell is automatically closed after the write-output commands

Running the same files from windows cmd gives no issues, but when the code is executed from python the PowerShell terminal is automatically closed How can I avoid this?

elpie89
  • 85
  • 9
  • If this isn't a duplicate then I'd suggest searching for a pause command that works in powershell. In CMD the pause command is literally just "pause". Not sure about PowerShell though. – Zach Pedigo Jul 22 '19 at 13:10

0 Answers0