Page 1 of 1
Launch a batch in another batch
Posted: 25 Jan 2013 03:59
by jvuz
Hi,
because my original file needs to be run as administrator (elevated rights), I created another batch file with this code
Code: Select all
@echo off
net user administrator password
runas /user:administrator 0-setup.bat
exit
The file 0-setup.bat is in the same directory where this file resides. But nothing opens.
What am I doing wrong?
Jvuz
Re: Launch a batch in another batch
Posted: 25 Jan 2013 04:36
by shirulkar
I think you should mention domain also
@echo off
net user administrator administrator
runas /user:domain\administrator zz.bat
exit /b
Re: Launch a batch in another batch
Posted: 25 Jan 2013 04:43
by jvuz
It's with the local (built-in) administrator
Re: Launch a batch in another batch
Posted: 25 Jan 2013 05:07
by foxidrive
What do you get on the console when you run this:
Code: Select all
@echo off
net user administrator password
runas /user:administrator 0-setup.bat
pause
Re: Launch a batch in another batch
Posted: 25 Jan 2013 05:12
by jvuz
This is the message:
Code: Select all
The command completed succesfully.
Enter the password for administrator:
Attempting to start 0-setup.bat as user "VM-J3\administrator" ...
Press any key to continue...
and after I hit a key, nothing happens
Jvuz
Re: Launch a batch in another batch
Posted: 25 Jan 2013 05:28
by foxidrive
so what is in 0-setup.bat?
Use this as 0-setup.bat to test it:
Re: Launch a batch in another batch
Posted: 25 Jan 2013 05:30
by jvuz
That's a script we use to install new pc's. But some commands won't pass, like writing something in the WIndows folder. That only works when you do it wiht elevated admin rights.
Re: Launch a batch in another batch
Posted: 29 Jan 2013 02:42
by john924xps
You could just insert this piece of code in the start of your original program, and it will prompt you if you want to run as administrator. It's not pure batch but it works.
Code: Select all
@echo off
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
<YOUR BATCH SCRIPT HERE>
Source:
https://sites.google.com/site/eneerge/scripts/batchgotadmin