Run As Administrator

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Run As Administrator

Post by MigrationUser »

05 Dec 2012 20:18
hjahmad

I am writing some scripts to implement settings using the secedit command. As i am hoping to run this is multiple XP and 7 machines from a CD, the admin privileges are messing things up. In the past i have gotten it to work easily on windows XP. But on 7. to use secedit, i need to run CMD as an administrator. It runs but now it cannot find /templates folder on the CD. It seems that once CMD is run as admin, it launches from the system32 folder. and ends up looking for the files in system32/templates, which is incorrect. the folder is located in the same directory as the .bat file.

Is it possible to script RunAs into the bat file and have it prompt for a local admin account for the computer the CD is running on?

I guess i could correct all the paths and write them all out begin with the drive letter, but first, i dont know how to tell it to look to the CD, second, I cant copy it to the desktop and run it from there if i needed too.

any other suggestions?

thanks for the help

----------------------------

#2 05 Dec 2012 23:37
RG

The current directory gets changed when you 'Run as admin'.
To prove that just put this temporary test line near the top of your program:

Code: Select all

echo.cd=%cd%
Now run normally and run again as admin.
No wonder it can't find anything because the current directory got changed underneath of you!
This is easily resolved by putting the following line near the top of your program.
Need to do this before you reference any files/folders.
Put it right before the above test line so you see proof.

Code: Select all

pushd "%~dp0"
Notice that the current directory is now what you would expect regardless of whether or not you 'Run as admin'.
I always put the following line at the end of the routine.

Code: Select all

popd
Last edited by RG (06 Dec 2012 01:14)

Windows Shell Scripting and InstallShield

----------------------------

#3 06 Dec 2012 16:21
hjahmad


thanks!
that worked
is there any advantage to placing the popd at the end of the script?

----------------------------

#4 06 Dec 2012 18:11
AiroNG

Yes and no :)
It depends if you continue working on the same console or not.

I only use DOS-consoles and never ever doubleklick .bat and .cmd files.
So in my scripts i use pushd/setlocal frequently and place popd/endlocal when the function is finished and i do not need another directory/variable any longer.

I guess it's basically "choose at your own liking" (or if your script suddenly goes awry :D )

Last edited by AiroNG (08 Dec 2012 19:14)

I don't suffer from insanity, I enjoy every minute of it.

----------------------------

#5 06 Dec 2012 19:00
RG

Basically what AiroNG said. Of course you are aware that you can't POPD too early because that will restore the current directory back to what it was before and you will again not be able to reference files/folders relatively!.

Good practice is to pair the pushd and popd. It may not matter for a simple script, can eventually cause trouble if you later end up calling this script numerous times and you are only pushing and never poping.

Another good reason to pair them is that if you PUSHD a network path, a temporary drive letter will be assigned (allocated from Z: on down). If you don't use POPD, that assignment remains and you may eventually use up all of your available drive letters.

Windows Shell Scripting and InstallShield

----------------------------

#6 06 Dec 2012 23:19
hjahmad

Oh Ok
not sure if it will affect me but ill throw it in anyways

Is there a way for the script to automatically Run as admin? Im looking for something that i can autorun from a cd and not have to run it as admin. If i need to enter a password or something, thats fine. But if im already logged in as an admin, i dont think i would have too.

Thanks for the help!

----------------------------

#7 07 Dec 2012 05:32
RG

Make a shortcut to the script
Right click on the shortcut
Properties
Select 'Shortcut' tab
Advanced
Check the "Run as administrator" box
Now always execute with the shortcut

Windows Shell Scripting and InstallShield

----------------------------

#8 07 Dec 2012 16:40
hjahmad

ahhh, i dont know why i didnt think of that

going back to the Pushd and popd

i added the start command to give the script a title, but it doesnt seem to work will with pushd. either before of after pushd, it goes and creates a new window, even if use /B.

----------------------------

#9 07 Dec 2012 18:56
RG

Don't need the START command if all you want is a title.
Just do this:

Code: Select all

title Whatever title you want goes here
Windows Shell Scripting and InstallShield

----------------------------

#10 19 Apr 2014 12:38
lovefoe


In Windows Vista, Windows 7 or Windows 8 with UAC turned on, and you run the batch file with %CD% to get current directory, it actually points to C:\windows\system32. To solve this, set the following code at the beginning of your batch file.

Code: Select all

@setlocal enableextensions
@cd /d "%~dp0"
It will change the directory or file path back to your current folder/path.
Post Reply