Problem:
I want to call my PowerShell script with elevated privileges in a non-administrator command prompt. When I manually open the Command Prompt with 'Run as Administrator' and enter the line:
powershell -ExecutionPolicy Unrestricted -Command "Start-Process Powershell -Verb RunAs -Wait -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File example.ps1 param1'"
My script PowerShell script runs just fine.
However, when I run it in a non-administrator command prompt, I see a PowerShell window appear for a split second and exits.
Any ideas on how to resolve this issue?
Tries
- Same thing happens, the window opens for a split second and then exits
Run Powershell script via batch file with elevated privileges
runas.exe /netonly /noprofile /user:domainadm@domain "powershell.exe -
noprofile -File "C:\Users\...\Desktop\Powershell_scripts\New-
ADuser\.ps1" -verb RunAs"
- Same thing happens here as well, the window opens for a split second and then exits
Run a Powershell script from Batch file with elevated privileges?
powershell -Command "&{ Start-Process powershell -ArgumentList '-File example.ps1 param1' -Verb RunAs}"
- Same issue, the window opens for a split second and then exits
Run a powershell script from cmd with elevated privileges and passing parameters
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath PowerShell.exe -Verb RunAs -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File "example.ps1 param1"'"
CONTEXT: My PowerShell script is for downloading a NSIS Installer specified at param1
EDIT: Script code:
$param1 = $args[0]
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms
$installer = Start-Process -FilePath $param1 -PassThru
$installerWindowName = "Installer Setup"
while (-not ($installer.MainWindowTitle -match "$installerWindowName")) {
Start-Sleep -Milliseconds 100
$installer.Refresh()
}