简体   繁体   English

通过vbscript或bat文件调用python启动新进程时获取未修改的环境变量

[英]Getting unmodified enviromental variables when starting new process via vbscript or bat file call to python

I have an application that has environment variables which I cannot control and are modified from the "default" environment variables applications are given when started from cmd or explore (or whatever). 我有一个应用程序,该应用程序具有无法控制的环境变量,并且已从“默认”环境变量中进行了修改,这些应用程序是从cmd或Explore(或其他)启动时给出的。 My application allows me to run vbscripts, but these scripts take the parent application's environmental variables, which is okay. 我的应用程序允许我运行vbscripts,但是这些脚本使用了父应用​​程序的环境变量,这没关系。 I would like to use the vbscript to start python with the "normal"/"default" environment variables most application get when started from cmd or explorer (or whatever). 我想使用vbscript从大多数应用程序从cmd或explorer(或其他)启动时获得的“正常” /“默认”环境变量来启动python。 I've attempted to use the vbscript to call a bat file that runs python, however python is still maintaining the environment variables of its grand-grand-parent. 我试图使用vbscript调用运行python的bat文件,但是python仍在维护其祖父母的环境变量。

How can I get a vbscript to call application (python) without passing its "modified" environment variables to its child? 如何在不将其“已修改”环境变量传递给子级的情况下让vbscript调用应用程序(python)? I've also tried using start /i , but because the vbscript's parent modified its variables before the vbscript started, it won't reset to a clean environment. 我也尝试过使用start /i ,但是由于vbscript的父级在vbscript启动之前修改了其变量,因此不会重置为干净的环境。

My VBScript looks something like this: 我的VBScript看起来像这样:

sub run

  dim wshShell

  Set wshShell = CreateObject( "WScript.Shell" )

  Dim cmd
  cmd = ""
  cmd = cmd & chr(34) ' double quote character'
  cmd = cmd & "startPython.bat"
  cmd = cmd & chr(34) 

  wshShell.run( cmd )
end sub

My application will start the run subroutine with modified undesirable environment variables. 我的应用程序将使用修改后的不良环境变量启动run子例程。

The startPython.bat file looks like this: startPython.bat文件如下所示:

  start /i python "python3file.py" %*

  rem pause

The batch file is not required, but seems like possible point that the environment chain could be broken, but it does not seem to. 批处理文件不是必需的,但似乎可以打破环境链,但事实并非如此。

In the end, I'd like to have the vbscript start python using the "default"/"normal" environment variables that new application started by the user via cmd or explorer would be given (I'm not picky about how old they are, or if they are the OS startup variables, or ones that are slightly modified during startup before any cmd or explorer, or user startup. These modifications are acceptable, however modifications made by my parent application should be gone. 最后,我想让vbscript使用“默认” /“正常”环境变量来启动python,该环境变量将给出用户通过cmd或Explorer启动的新应用程序(我不挑剔它们的年龄。 ,或者它们是操作系统启动变量,或者是在任何cmd或资源管理器或用户启动之前的启动过程中稍作修改的变量,这些修改是可以接受的,但是我的父应用程序所做的修改应该被删除。

(It's also important that the session environment variables of the parent DO NOT change, however after pages of stackoverflow and other readings, that seems to be difficult (and non-recommended) anyways.) (也很重要的一点是,父级的会话环境变量不要更改,但是在页面溢出和其他读数之后,无论如何这似乎都是困难的(并且不推荐)。)

(I understand their isn't a "default" or "normal" set of environment variables, but there exists system level and user level, either of which would be acceptable in my situation, as long as I'm not using the session variables created by my parent application. Acceptable solutions would also include "stealing" a copy of the environment variables, but a fresh set of OS generated environment variables would be most preferred (either of the system level or user level type).) (我知道它们不是一组“默认”或“正常”的环境变量,但是存在系统级别和用户级别,只要我不使用会话变量,这两种情况在我的情况下都是可以接受的可以接受的解决方案还包括“窃取”环境变量的副本,但是最好使用一组新的OS生成的环境变量(系统级别或用户级别类型)。

An ideal solution would be limited to vbscript (optionally via bat files) to start python with clean set of environment variables. 理想的解决方案将仅限于vbscript(可选通过bat文件)以使用干净的环境变量集启动python。

EDIT Additional updates: 编辑其他更新:

Based on Harry Johnston suggestions: 根据哈里·约翰斯顿的建议:

I tried using ShellExecute by modifying the VBScript to look something like: 我试着通过修改VBScript来使用ShellExecute,如下所示:

  dim objShell
  set objShell = CreateObject("shell.application")
  objShell.ShellExecute cmd , "" , "blahBlahBlah"  
  set objShell = nothing

Based on the msdn link suggestion, but I'm setting getting a modified environmental variable session. 基于msdn链接的建议,但是我正在设置获取修改后的环境变量会话。

(I also removed /i then start from the batch script file in case they were getting the parents session variables.) (我也删除/然后在情况下,他们都拿到父母会话变量的批处理脚本文件开始 。)

You want the ShellExecute method of the shell.application object. 您需要shell.application对象的ShellExecute方法。

This causes the Windows shell (aka Windows Explorer, more or less) to open the specified application or file on your behalf. 这将导致Windows Shell(或多或少又称为Windows资源管理器)代表您打开指定的应用程序或文件。 That means the newly launched application gets a fresh set of environment variables, just as if you'd launched it by double-clicking in Explorer. 这意味着新启动的应用程序将获得一组新的环境变量,就像您通过双击资源管理器启动它一样。

Inheritance of environment variables is a built-in concept of Windows. 环境变量的继承是Windows的内置概念。 However, anyone calling the CreateProcess() function can specify the environment variables for that process ( lpEnvironment ). 但是,任何调用CreateProcess()函数的人都可以为该进程指定环境变量( lpEnvironment )。 Problem is: how to know what the default environment variables are? 问题是:如何知道默认环境变量是什么?

The only workaround I see at this time, is to create a task in task scheduler and having the command executed by task scheduler instead of your own application. 我目前看到的唯一解决方法是在任务计划程序中创建任务,并由任务计划程序而不是您自己的应用程序执行命令。 This approach assumes that task scheduler is started with an original set of environment variables. 该方法假定任务调度程序是用一组原始环境变量启动的。 Commands you'll need for that: 您需要的命令:

schtasks /create ...
schtasks /run ...
schtasks /delete ...

Setting it all up is a bit tricky. 全部设置有点棘手。 I can't tell whether that works, but maybe you can give it a try. 我无法确定是否可行,但也许您可以尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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