JJ
2019-03-20 14:31:00 UTC
I'm trying to write a script for Win7 that will run a program with elevated
priviledge. Just like what the "Run as administrator" context menu does. I
know that this is done by using shell with the "RunAs" verb instead of the
default "Open" verb. However with "RunAs" verb, the working directory
argument passed to the ShellExecute is ignored, and I always end up getting
the Windows' SYSTEM32 directory as the working directory instead.
Here's the "elevate.vbs" script. Note: everything here are minimal code for
the sake of reproducing the problem.
cd = createobject("wscript.shell").currentdirectory
wscript.echo "elevate.vbs: " & cd
set sh = createobject("shell.application")
sh.shellexecute wscript.arguments(0), wscript.arguments(1), cd, "runas", 1
Here's the "test.vbs" script that I use for testing.
cd = createobject("wscript.shell").currentdirectory
wscript.echo "test.vbs: " & cd
When I tested it, I use non elevated Command Prompt with "E:\WORK" as the
current directory. All of the script files are in this directory. If I use
below command line:
elevate wscript test.vbs
"elevate.vbs" script shows the message box with the correct working
directory:
elevate.vbs: e:\WORK
But the elevated WSCRIPT complained with this error message box:
Can not find script file "C:\Windows\system32\test.vbs".
Now if I use full path to the "test.vbs" file like this:
elevate wscript e:\work\test.vbs
"elevate.vbs" script shows the same message as expected, but the elevated
"test.vbs" script shows this message:
test.vbs: C:\Windows\system32
Meaning that the "RunAs" verb did ignored or messed up the given working
directory. FYI, if the Command Prompt is already elevated, everything works
fine. So is there a solution other than manually readjusting the working
directory?
priviledge. Just like what the "Run as administrator" context menu does. I
know that this is done by using shell with the "RunAs" verb instead of the
default "Open" verb. However with "RunAs" verb, the working directory
argument passed to the ShellExecute is ignored, and I always end up getting
the Windows' SYSTEM32 directory as the working directory instead.
Here's the "elevate.vbs" script. Note: everything here are minimal code for
the sake of reproducing the problem.
cd = createobject("wscript.shell").currentdirectory
wscript.echo "elevate.vbs: " & cd
set sh = createobject("shell.application")
sh.shellexecute wscript.arguments(0), wscript.arguments(1), cd, "runas", 1
Here's the "test.vbs" script that I use for testing.
cd = createobject("wscript.shell").currentdirectory
wscript.echo "test.vbs: " & cd
When I tested it, I use non elevated Command Prompt with "E:\WORK" as the
current directory. All of the script files are in this directory. If I use
below command line:
elevate wscript test.vbs
"elevate.vbs" script shows the message box with the correct working
directory:
elevate.vbs: e:\WORK
But the elevated WSCRIPT complained with this error message box:
Can not find script file "C:\Windows\system32\test.vbs".
Now if I use full path to the "test.vbs" file like this:
elevate wscript e:\work\test.vbs
"elevate.vbs" script shows the same message as expected, but the elevated
"test.vbs" script shows this message:
test.vbs: C:\Windows\system32
Meaning that the "RunAs" verb did ignored or messed up the given working
directory. FYI, if the Command Prompt is already elevated, everything works
fine. So is there a solution other than manually readjusting the working
directory?