Run the batch as an administrator (auto-raise) and then uncheck

I have a script that works in 2 parts. The first part requires administrator access (updates the HOSTS file and performs some copy / overwrite operations). After this part ends, I need to map the drive using the node alias to the first part of the updated script.

I figured out how to get elevated privileges using this SO question . But disk mapping (while at the administrator) displays the disk in an administrator session. I need to β€œrelease” back to user mode in order to run the second script.

This script I run at least once a day and possibly several times a day. I am trying to create a solution that is only 1.bat file if possible. For reasons, the scripts are written in perl.

What I tried:

  • Using runas / user: regular_user command (this does not work)
  • 1 bat file Using CALL for two batch files (this "works", but for some reason both run simultaneously)
  • Run two bat files individually and manually.
  • Search SO, but I could not find the administrator-> user, but only user-> admin

TL; DR: How to disable user mode from administrator mode in a batch file?

+6
source share
3 answers

It is best to use the best third-party remote / local execution tool: Windows Sysinternals PSEXEC. You can provide credentials and do what you need using PSEXEC! You can embed PSEXEC commands in your batch file or vbs and run them without a hitch. You can also invoke one PSEXEC higher resolution command and the next without any increase, while mixing credentials in one unique batch file.

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

+2
source

If you use 2 batch files, call the ElevatedBatch.cmd package with a height mark using Main.cmd (which continues to do unleaded things):

 @ECHO OFF START /WAIT ElevatedBatch.cmd %1 %2 %3 %4 %5 %6 %7 %8 %9 REM here you can do unelevated stuff: ECHO Running unelevated now 

The /WAIT ensures that the script will wait for the end of ElevatedBatch.cmd . For ElevatedBatch.cmd you can use a template like this one to pick it up.

+2
source

Make the non-raised part first, then lift and continue.

0
source

Source: https://habr.com/ru/post/949569/


All Articles