Run batch as Admin (auto-elevate) and then de-elevate

batch-file, command-prompt, perl, windows

Solution

Your best bet is to use the best third party remote/local execution tool : Windows Sysinternals PSEXEC. You can supply credentials and accomplish what you need using PSEXEC! You can put PSEXEC commands into your batch file or vbs and have them run without a hitch. You can also call one command with PSEXEC elevated permission and the next without any elevation, while mixing credentials in a single unique batch file.

http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

Problem

I have a script that runs in 2 parts. The first part requires admin access (updates HOSTS file and does some copying/overwriting). After that part finishes, I need to map a drive using the hostname alias the first part of the script updated. I have figured out how to get the elevated privileges by using this SO Question. But mapping a drive (while in admin) maps a drive into the admin's session. I need to "de-elevate" back into user mode to run my second script. This is a script I run at least once every day, and possibly multiple times per day. I am trying to create a solution that is just 1 .bat file, if possible. For reasons, the scripts are written in perl. Things I have tried: - Using the runas /user:regular_user command (this does not work) - 1 bat file Using CALL for the 2 batch files (This "works" but for some reason both run at the same time) - Running 2 bat files separately, and manually. - Searching SO, but I could not find admin->user instead only user->admin TLDR: How do I de-elevate to user mode from admin mode in a batch file?

Original source

Related problems