Jump to content

Run a batch file with adm rights


vitorio

Recommended Posts

I will like to run a bat file with administrator privileges as below:

 

WindowServices.bat

Here instructions to run bat file with adm rights

CD \Windows\System32

net start WindowsAudio AudioSrv

Pause

 

I do not wish to be ask if I want to run with administrator privileges, but within the bat file, please.

Thanks.

 

Link to comment
Share on other sites


  • Replies 7
  • Views 1.1k
  • Created
  • Last Reply
  • Administrator

Right click the file > Properties > Shortcut > Advanced > Run as administrator.

 

This will always run the file as administrator there.

Link to comment
Share on other sites


You can implement the BatchGotAdmin script for this code :

 

Quote

echo Checking for Administrator rights.

>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

if '%errorlevel%' NEQ '0' (
    echo Not running in Administrator mode. && goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt

    echo Requesting Administrator rights.

    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    exit /B

:gotAdmin

    echo Running as Administrator.

    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"
:----------------------------------------------------------------------------

 

net start WindowsAudio

pause

 

 

Some feedback :

 

Your current implementation is error-prone since you don't check if the service exists before trying to run it, and you don't check if the service is running after the net start command (look up StackOverflow, that's an invaluable source of information for any technical question).

 

You should use @echo off so you don't have disrupting lines in the command prompt window showing the commands.

 

You can then manually set status messages for your commands with echo, to create a blank line use echo. (Note the dot right after).

 

You can also hide a command's output by adding a >NUL after it :

 

net start WindowsAudio >NUL

 

Sometimes it still shows a message, so you have to hide the error output as well with 2>NUL :

 

net start WindowsAudio >NUL 2>NUL

 

I see you set the CD to system32, but you don't need to : any program in the system32 folder cans be invoked merely with its filename (net, icalcs, reg, etc).

 

However if your filename isn't unique you can do that to avoid running the wrong file.

 

Also, use %WINDIR% when you're querying the Windows folder, so you don't have drive letter problems (I had a computer where the drive letter was F:)

 

cd %WINDIR%\system32

 

Finally, I think the service you want to start is audiosrv and not WindowsAudio, but I might be wrong.

 

 

Link to comment
Share on other sites


straycat19

Why are you trying to reinvent the wheel? 

 

There are a dozen easier ways to do what you are trying to do.  Read options 5 thru 8 at 

https://www.sevenforums.com/tutorials/11841-run-administrator.html

 

Or take a look at another permanent solution here

https://sourceforge.net/projects/raacrunasadmin/

 

RAAC allows you run any application or program without the need to enter a username or password. this program helps many IT admins and Computer Tech's who are in the field dealing with many computer systems who need to manage.

 

Features
Searches for Installed Programs on the System for easily adding programs
Has Startup Feature that starts programs as admin when windows boots up.
Can save application's in RAAC so you can easily click and run without hassel.

Link to comment
Share on other sites


10 hours ago, Rekkio said:

You can implement the BatchGotAdmin script for this code :

 

 

Some feedback :

 

Your current implementation is error-prone since you don't check if the service exists before trying to run it, and you don't check if the service is running after the net start command (look up StackOverflow, that's an invaluable source of information for any technical question).

 

You should use @echo off so you don't have disrupting lines in the command prompt window showing the commands.

 

You can then manually set status messages for your commands with echo, to create a blank line use echo. (Note the dot right after).

 

You can also hide a command's output by adding a >NUL after it :

 

net start WindowsAudio >NUL

 

Sometimes it still shows a message, so you have to hide the error output as well with 2>NUL :

 

net start WindowsAudio >NUL 2>NUL

 

I see you set the CD to system32, but you don't need to : any program in the system32 folder cans be invoked merely with its filename (net, icalcs, reg, etc).

 

However if your filename isn't unique you can do that to avoid running the wrong file.

 

Also, use %WINDIR% when you're querying the Windows folder, so you don't have drive letter problems (I had a computer where the drive letter was F:)

 

cd %WINDIR%\system32

 

Finally, I think the service you want to start is audiosrv and not WindowsAudio, but I might be wrong.

 

 

Sorry. You are right, the name is WindowsSrv. I use the display name instead of the service name.

I am going to try the script you proposed.  I will let you know the results.

Thanks.

 

Link to comment
Share on other sites


3 hours ago, straycat19 said:

There are a dozen easier ways to do what you are trying to do.  Read options 5 thru 8 at 

https://www.sevenforums.com/tutorials/11841-run-administrator.html

 

Or take a look at another permanent solution here

https://sourceforge.net/projects/raacrunasadmin/

I will try the scheduler option suggested. Or the third part option.

I did to used them as a first option since many IT corporate security departments blocked this options in their computers.

I think the only option they can not blocked is running the script in the bat file. 

I may be wrong but that's my believe from my work experience a few years back.

Thanks for the help.

Link to comment
Share on other sites


Quote

I think the only option they can not blocked is running the script in the bat file. 

I may be wrong but that's my believe from my work experience a few years back.

Actually if your SYSADMIN uses GROUP POLICIES, I think BAT file execution will be one of the first things they block

Link to comment
Share on other sites


@RekkioThe script works flowless. Just run it this morning It did what I needed.

Thanks for the script and the explation to understand how it works. It has been very useful to me. Otherwise I had to do it manually each time I boot or restart the computer. Thanks for your help.

 

Link to comment
Share on other sites


Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...