1
votes

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:

  1. Using the "runas /User:Administrator ..." command but it ask for the Administrator password: Fail
  2. Using the "ShellExecute ...., "runas"" command but it tells me that my script is not an application: Fail
2
Try using runas on cmd.exe or wscript.exe instead of directly on the script. - Harry Johnston
@Harry I had tried runas but I have the following caveat: we don't know the Administrator password of our machine. But each of us as administrative rights (to some extent) So I can modify the sys env var from the Windows configuration GUI (after being prompt the UAC). But I cannot use the runas command with my own user, I get a permission denied. And I cannot run it as Administrator as I don't know the password... I really need a simple privilege elevation, just like when I want to perform an administrative task where I simply have to confirm the change. Anyway, thanks for the suggestion :) - Huygens
I thought the runas verb did the same thing as "run as administrator", i.e., if you're already an administrator it just prompts for approval? - Harry Johnston
That's also what I had hoped for :( - Huygens

2 Answers

1
votes

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:

currentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))

Set UAC = WScript.CreateObject("Shell.Application")
UAC.ShellExecute "wscript.exe", currentDirectory + "my-script.vbs /Param1:Value1 ...", "", "runas", 0

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.

0
votes

IMHO, disable UAC, it's just a pain in the * But if you can't (like me 8<), you can use

psexec.exe -d -u userid -p password CMD /c program_with_path

You (or the user where the sript runs) will have to confirm the prompt though.