简体   繁体   中英

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). My application allows me to run vbscripts, but these scripts take the parent application's environmental variables, which is okay. 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). 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.

How can I get a vbscript to call application (python) without passing its "modified" environment variables to its child? 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.

My VBScript looks something like this:

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.

The startPython.bat file looks like this:

  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.

(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).)

An ideal solution would be limited to vbscript (optionally via bat files) to start python with clean set of environment variables.

EDIT Additional updates:

Based on Harry Johnston suggestions:

I tried using ShellExecute by modifying the VBScript to look something like:

  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.

(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.

This causes the Windows shell (aka Windows Explorer, more or less) to open the specified application or file on your behalf. 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. However, anyone calling the CreateProcess() function can specify the environment variables for that process ( 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.

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