在升高的窗口中调用BAT,然后更改ENV变量 [英] Call a BAT in an elevated window and changing ENV variables before

查看:241
本文介绍了在升高的窗口中调用BAT,然后更改ENV变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PowerShell脚本中,我必须在高架窗口中调用批处理文件。
所以我做

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

Start-Process my.bat -Verb runas

现在, my.bat 希望有一些ENV变量在原来设置窗口。
但是,由于升高的窗口以admin身份执行,因此我设置为常规用户的变量似乎不会在管理窗口中设置。

Now, my.bat expects to have some of the ENV variables I set on the original window. However, since the elevated window is executed as admin, those variables I set as a regular user don't appear to be set on the admin window.

有没有办法在调用 my.bat 之前在管理窗口中设置ENV var?

Is there a way to set ENV vars in an admin window prior to calling my.bat?

推荐答案

你想要的是不可能的。出于安全考虑,升级的进程不会继承父级的环境。您可以做的是创建一个运行提升的包装脚本,并在运行 my.bat 之前,让该脚本从参数中设置环境变量。

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.

默认情况下,IIRCrunas未启用PowerShell脚本,因此包装器脚本必须是批处理文件:

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"

像这样运行:

Start-Process .\wrapper.ps1 -ArgumentList 'foo', 'bar' -Verb runas

这篇关于在升高的窗口中调用BAT,然后更改ENV变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆