简体   繁体   中英

How to pass arguments to PowerShell script executed as administrator from batch script?

I have a PowerShell script that needs to be executed using Windows' Batch Script as Administrator. The script is running without arguments, however when I pass arguments, the PowerShell window pops up and closes instantly. Here is what I have tried.

Batch script

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell '-NoProfile - 
ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1" -FolderPath \"test"'   -Verb 
RunAs"

Powershell

param
(
 [string]$FolderPath ='D:\batch_scripting'
 )

echo $FolderPath
pause

I will add further functionalities in these scripts later. But I have to figure out this first. The script can be executed if -FolderPath argument is not passed.

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell '-NoProfile - 
ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1"' -Verb 
RunAs"

I have also gone through following questions but this does not work for me.

I found the solution, although it's weird. When you execute the powershell script as Administrator, trailing "/" must be added to the path of the script.

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell 'NoProfile - ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1\"' -Verb RunAs"

It works fine now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM