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?

Kruug
  • 5,222
  • 3
  • 22
  • 30
Stagger
  • 75
  • 2
  • 2
  • 10
  • 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 Jan 22 '14 at 17:01
  • Why are you start and stopping that service every 5 minutes? – and31415 Jan 23 '14 at 02: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 Jan 23 '14 at 06: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= – Mark Deven Jun 27 '18 at 19:58

3 Answers3

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.

Matthew Williams
  • 4,314
  • 12
  • 26
  • 38
  • you assume right. – Stagger Jan 22 '14 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 Jan 22 '14 at 15:59
  • oh there i cant even post it in comment, i will post it as answer – Stagger Jan 22 '14 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? – Matthew Williams Jan 22 '14 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 Jan 22 '14 at 16:03
  • So here you seem to have a syntax error with the RUNAS command. Ensure you changed the line to suit your specific account and requirements. This page will help you construct what you require. http://ss64.com/nt/runas.html EDIT: I also noticed your spelling of Restrat.bat. Unless you have actually misspelled the actual filename to? – Matthew Williams Jan 22 '14 at 16:16
  • I did not misspell is, that's how it is in the file name. i adjusted where needed to fit my situation but still does not work. I can't understand it. – Stagger Jan 22 '14 at 16:35
  • Note: while testing this, i noticed that a PC on which my batch said "access denied" earlier, now runs the batch file with no problem and i haven't installed additional programs or software. How could this have happened? – Stagger Jan 22 '14 at 16:36
  • Right take the runas line from your script and return it to it's original state. Create an new batch file and use the runas line and point it at the original file. See if that works. Bit of a hack I know. – Matthew Williams Jan 22 '14 at 16:51
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...

JSanchez
  • 1,682
  • 13
  • 10
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 . . .

Stagger
  • 75
  • 2
  • 2
  • 10