0

I created a batch file which stops a service,then stops a process and then starts the service it stopped.

When I execute the batch file, it says "access denied". I cannot figure out why it says access denied because I am an Administrator. I searched everywhere for a solution to this with no luck.

Here are my command lines within the batch file:

net stop uvnc_service
taskkill /f /im winvnc.exe
net start uvnc_service

Note: When I right click on it, I can say "run as administrator" which then works, but I created a task in task scheduler which executes the batch file every 5 minutes and I cant right click and say "run as administrator" every 5 minutes.

Also in the properties of the batch file under the compatibility tab, "run as administrator is greyed out so I cant check it.

How can I automatically run the batch file as administrator every 5 minutes?

Is there maybe a command line I can insert into my batch file that runs it as administrator every time?

4
  • You need to elevate the user access to administrator. run a cmd program tha elevates you and then run the batch.. make a wrapper batch that does both operations..
    – ilansch
    Commented Jan 22, 2014 at 17:01
  • Why are you start and stopping that service every 5 minutes?
    – and31415
    Commented Jan 23, 2014 at 2:10
  • it's the UltraVNC's service and it keeps crashing after time so i'm restarting it every 5 minutes so it doesn't crash
    – Stagger
    Commented Jan 23, 2014 at 6:17
  • I like adding this to the top of my files: if not "%1"=="am_admin" (echo Loading Admin... & echo %cd%>C:\users\Public\CDT.txt & powershell start -verb runas '%0' am_admin & exit /b) Then below that set /p cdd=<C:\users\Public\CDT.txt and cd %cdd% . The problem is that batch files do not run as administrator unless you tell them to, regardless of the user.
    – Mark Deven
    Commented Jun 27, 2018 at 19:58

3 Answers 3

1

Might this help you? I assume you already have the file running every 5 minutes and just need some method of adding the right admin rights.

https://stackoverflow.com/questions/18755553/automatically-running-a-batch-file-as-an-administrator

EDIT: You should also be able to set "run with higher privileges" in the scheduler which might solve it for you.

9
  • you assume right.
    – Stagger
    Commented Jan 22, 2014 at 15:58
  • I just tried what you said and it doesn't work, here is what it says in CMD after i execute it:
    – Stagger
    Commented Jan 22, 2014 at 15:59
  • oh there i cant even post it in comment, i will post it as answer
    – Stagger
    Commented Jan 22, 2014 at 16:00
  • Is that with adding the admin line to the batch file or setting the higher privilege under "General" on the task scheduler? Commented Jan 22, 2014 at 16:00
  • it is with adding the admin line to the batch file. it is already set to use highest privileges in task scheduler.
    – Stagger
    Commented Jan 22, 2014 at 16:03
1

A couple of suggestions:

  1. Have you tried the MakeMeAdmin.cmd script by Aaron Margosis? A great way to learn how to re-start your script with elevated privileges.
  2. How about Matt's solution found at StackOverflow?

Hope that helps somewhat...

0

Here is what it says in CMD after i execute it:

C:\Users\VANTO.JACQUES\Documents>runas /savecred /profile /user:Administrator VN C Restrat.bat RUNAS USAGE:

RUNAS [ [/noprofile | /profile] [/env] [/savecred | /netonly] ] /user: program

RUNAS [ [/noprofile | /profile] [/env] [/savecred] ] /smartcard [/user:] program

RUNAS /trustlevel: program

/noprofile specifies that the user's profile should not be loaded. This causes the application to load more quickly, but can cause some applications to malfunction. /profile specifies that the user's profile should be loaded. This is the default. /env to use current environment instead of user's. /netonly use if the credentials specified are for remote access only. /savecred to use credentials previously saved by the user. This option is not available on Windows 7 Home or Windows 7 Starter Editions and will be ignored. /smartcard use if the credentials are to be supplied from a smartcard. /user should be in form USER@DOMAIN or DOMAIN\USER /showtrustlevels displays the trust levels that can be used as arguments to /trustlevel. /trustlevel should be one of levels enumerated in /showtrustlevels. program command line for EXE. See below for examples

Examples:

runas /noprofile /user:mymachine\administrator cmd runas /profile /env /user:mydomain\admin "mmc %windir%\system32\dsa.msc" runas /env /user:[email protected] "notepad \"my file.txt\""

NOTE: Enter user's password only when prompted. NOTE: /profile is not compatible with /netonly. NOTE: /savecred is not compatible with /smartcard.

C:\Users\VANTO.JACQUES\Documents>pause Press any key to continue . . .

1
  • Put quotes around the file name: "VNC Restart.bat"
    – Kruug
    Commented Jan 22, 2014 at 16:53

Not the answer you're looking for? Browse other questions tagged .