简体   繁体   中英

Using RUNAS to use the local Administrator account to run a second batch file with elevated privileges

I am trying to develop a simple script that can run a second script as the machine's local administrator. As the first script I am currently trying:

set name=%computername%
runas /noprofile /user:%name%\Administrator "cmd.exe /c %cd%bg2.2.bat"    

When I run the script it never seems to run the second script, which is located in the same directory. The second script is only for copying files.

del "C:\Windows\obh_logo.bmp"
copy "%cd%obh_logo.bmp C:\Windows\obh_logo.bmp"

When the first script runs I am prompted for the admin password as expected and once I enter it, a second command prompt seems to flicker on the screen then vanishes. It never appears to run the second script. What seems to be the reason for this not working?

%cd% will probably not point where you think it does, (just tested it, of course the variable gets replaced before the command gets called and does actually point to the first scripts dir, but the problem still persists in the second script)
the runas changes the working directory for the cmd.exe instance you are starting, probably to C:\\Windows\\system32.
replace it with %~dp0 , which yields the drive and path of the current batch file.

But! Your script will not run the second file elevated, just under the Administrator account, which is not the same. Manual confirmation is always necessary, see here for possible ways to request elevation from within your script.

I now assume the missing rights is the actual problem, add a pause statement in your second script to watch it, or call it with cmd /k so the console doesn't close.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM