Home > database >  Running Batch File To Execute PowerShell and Additional Batch file
Running Batch File To Execute PowerShell and Additional Batch file

Time:03-24

I have the following in a Powershell script - InstallApp.ps1 - to download an executable, then install the executable, and finally, to run a batch file to apply the necessary configurations within the app:

#Change directory to script location
    CD $PSScriptRoot

#Download Application
    $AppSource = "www.url.com/application.exe;
    $AppDestination = "$PSScriptRoot\application.exe"
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    Invoke-WebRequest -URi $AppSource -OutFile $AppDestination

#Install Application

    .\application.exe --platform minimal --script silent-install.js InstallDir=C:\Application

#Configure Application

    Start-Process .\ConfigureApp.bat -Verb RunAs

    Write-host "Configuring Application. Do not proceed until addittional Command Prompt window closes"

    Pause

If I open PowerShell as Administrator and run this script, everything works without issue. The problem is, I need to pass this on and have other people run it as admin. My go to in this situation is to create a batch file called _RunMeAsAdmin.bat and include it in the package. With that I will have:

@echo off

:: Install App
cd /D "%~dp0"
Powershell.exe -ep bypass -file InstallApp.ps1 -Verb RunAs

When I run this, the Powershell script goes all the way through installing the application, but never calls the additional ConfigureApp.bat to finalize the configurations.

I realize this is probably a roundabout way of accomplishing what I want, but curious if anyone has any input on how I can get this to work?

CodePudding user response:

powershell.exe, the Windows PowerShell CLI, doesn't directly support -Verb RunAs in order to launch a process with elevation (as admin).

Instead, you must use use the -Command (-c) parameter to pass a command that calls Start-Process -Verb RunAs, which in turn requires a nested powershell.exe call in order to execute the .ps1 file with elevation:

powershell.exe -noprofile -c Start-Process -Verb RunAs powershell.exe '-ep bypass -file \"           
  • Related