Batch load Powershell script bypassing execution policy, with Powershell script that then self-elevating

0

The below code does bypass the execution policy for this one script, and prompt a Powershell process to elevate, but that elevated window disappears within seconds showing no output.

read-host put into the elevated-already if before the exit, or below it doesn't halt the script to see if there is any error message output before the window disappears.

Commenting out the exit causes the script to continue with all commands, but obviously without elevation some cmdlets that need to be run as admin don't function as intended.

I am not familiar enough with the format of the Powershell start-process call (Specifically the '"{0}'" part) to quickly debug this. What am I missing to make the batch file launch the Powershell script

1) Batch file to launch powershell bypassing execution policy

Powershell.exe -ExecutionPolicy Bypass -File Prep_Workstations.ps1

2) Inside Prep_Workstations.ps1, From https://ss64.com/ps/syntax-elevate.html:

If (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
  #Relaunch as an elevated process:
  Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path) -Verb RunAs
  exit
}

<rest of script goes here>

user66001

Posted 2019-12-14T00:59:00.460

Reputation: 1 050

Inside Prep_Workstations.ps1, try this modified command: Start-Process -FilePath powershell.exe -ArgumentList "-ExecutionPolicy", "Bypass", "-noexit", "-File", ('"{0}"' -f $MyInvocation.MyCommand.Path) -Verb RunAs – JosefZ – 2019-12-15T22:38:38.927

No answers