Don Exo
Don Exo

Reputation: 75

How to add administrator rights in a .bat file?

How to add administration rights in a simple .bat file?

I'm changing manually IP and DNS address to a specific one, but when I started it does nothing, surely because of the lack of "Run as Administrator" right. I know I can always run the .bat file as Administrator but from other reasons that won't help me a lot.

So what I basically need is something like this:

<command to set Administrator rights>
netsh interace ip set address name="Ethernet" static x.x.x.x y.y.y.y z.z.z.z
netsh interface ip set dns name="Ethernet" static x.x.x.x

Furthermore, if this works, can it be somehow made to restart the Ethernet adapter until it is at the specified Network-name ?

For example: If it show Network 6 to restart and restart until it reaches Network 3? Simple loop:

While(Ethernet.name != "Network 3")
{
Ethernet.restart();
}

I think you get the point what the question was.

Sorry for any mistakes.

Upvotes: 1

Views: 1838

Answers (3)

user2605194
user2605194

Reputation:

I know this doesn't so much relate to what you asked, but I just felt like I needed to share:

@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...     Click yes.
   goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
   echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
   set params = %*:"=""
   echo UAC.ShellExecute "%~s0", "%params%", "", "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>

Upvotes: 0

David Candy
David Candy

Reputation: 743

The more general way if psexec is undesirable due to corporate policy is to schedule it as a task. See

schtasks /create /?

and note /rl

Upvotes: 0

Flash Thunder
Flash Thunder

Reputation: 12045

Use psexec from Microsoft Administrative Tools to run process as Administrator from command line, as runas does not accept Administrator password as parameter.

http://technet.microsoft.com/pl-pl/sysinternals/bb897553.aspx

Upvotes: 1

Related Questions