Call a BAT in an elevated window and changing ENV variables before

后端 未结 1 1442
不思量自难忘°
不思量自难忘° 2021-01-20 15:01

In a PowerShell script, I have to call a batch file in an elevated window. So I do

Start-Process my.bat -Verb runas

Now, my.bat

相关标签:
1条回答
  • 2021-01-20 15:48

    What you want isn't possible. For security reasons elevated processes don't inherit the parent's environment. What you can do is create a wrapper script that you run elevated and have that script set the environment variables from parameters before running my.bat.

    IIRC "runas" isn't enabled for PowerShell scripts by default, so the wrapper script would have to be a batch file:

    @echo off
    
    set "VARIABLE1=%1"
    set "VARIABLE2=%2"
    
    call "C:\path\to\my.bat"
    

    Run it like this:

    Start-Process .\wrapper.ps1 -ArgumentList 'foo', 'bar' -Verb runas
    
    0 讨论(0)
提交回复
热议问题