4

I have written a batch file which starts OpenVPN. If the client is found, I check the connectivity by running PING. If the server is not found, then this means that although OpenVPN is running, the connection has not been established. At this point I want to restart OpenVPN. Because I am not using the admin account for this script, I always get the error "permission denied" when using TASKKILL /F. Is there anyway around this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Robert Buckley
  • 11,196
  • 6
  • 24
  • 25

2 Answers2

2

There are two methods of running a command with administrator privileges.

Solution 1:

Right-Click the batch-file and click run as admin.

Solution 2:

Use the runas command to run a command or batch file with administrative permissions. This way the process is still automate but you will still need to type in the password for administrator.

Example:

C:\> runas /user:administrator "taskkill /f /im explorer.exe"
Enter the password for administrator:

Type runas /? for more info.

Monacraft
  • 6,510
  • 2
  • 17
  • 29
  • There's a third. If you're logged on as an admin user but UAC is forcing your script to run without admin privileges, you can have your script [prompt for elevation](https://sites.google.com/site/eneerge/home/BatchGotAdmin) if needed. And there are surely others involving psexec and other third party tools. – rojo Jan 27 '15 at 13:59
1

if you are already the administrator, you can create a shortcut to your batch file and when you right click the shortcut -> properties -> advanced -> check the checkbox "run as administrator" and voila! everytime you run the batch script via this shortcut file, it will run as administrator and will kill without any discrimination, happily ever after

gawkface
  • 2,104
  • 2
  • 27
  • 29
  • This does not exist in Windows 10 – Kalamalka Kid Jul 04 '21 at 05:57
  • @KalamalkaKid its there in Win 10 (I have Home edition) when you right click the shortcut and not the original file. In shortcut's properties, go to `Shortcut` -> `Advanced` and there's the checkbox – gawkface Jul 04 '21 at 19:59