Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
jvuz
- Posts: 38
- Joined: 25 Feb 2011 03:37
#1
Post
by jvuz » 25 Jan 2013 03:59
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
-
shirulkar
- Posts: 40
- Joined: 15 Jan 2013 23:53
#2
Post
by shirulkar » 25 Jan 2013 04:36
I think you should mention domain also
@echo off
net user administrator administrator
runas /user:domain\administrator zz.bat
exit /b
-
jvuz
- Posts: 38
- Joined: 25 Feb 2011 03:37
#3
Post
by jvuz » 25 Jan 2013 04:43
It's with the local (built-in) administrator
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#4
Post
by foxidrive » 25 Jan 2013 05:07
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
-
jvuz
- Posts: 38
- Joined: 25 Feb 2011 03:37
#5
Post
by jvuz » 25 Jan 2013 05:12
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
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#6
Post
by foxidrive » 25 Jan 2013 05:28
so what is in 0-setup.bat?
Use this as 0-setup.bat to test it:
-
jvuz
- Posts: 38
- Joined: 25 Feb 2011 03:37
#7
Post
by jvuz » 25 Jan 2013 05:30
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.
-
john924xps
- Posts: 65
- Joined: 08 Jun 2012 07:48
#8
Post
by john924xps » 29 Jan 2013 02:42
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