I have to create a script which updates a system environment variable (based on a command line parameter) before launching a program.
In Windows 7, updating the system environment variable is denied. I would like to perform a privilege elevation for just the setting of the env. var. But run the program as a normal user.
How to do it?
Note:
I’ve tried the following solution:
Using 2 scripts:
- 1 master which get all information from command line, which call the slave script to change the system env. var., and which finally launch the program
- 1 slave script that update the system env. var.
- the master script tries to call the slave script using privilege elevation, but that does not work
I’ve try 2 solutions for the privilage elevation:
- Using the “runas /User:Administrator …” command but it ask for the Administrator password: Fail
- Using the “ShellExecute …., “runas”” command but it tells me that my script is not an application: Fail
I found a way that is working at least on Windows 7 (don’t know if it will work on the few Windows XP hat we still have around).
I did the following from the main script:
And the my-script is doing the sys var env update.
Note: My fist experience with ShellExecute failed because I was trying to execute the script. Instead of “wscript.exe” I had “my-script.vbs” for the executable name.