1

I have a batch file to start an application as a Windows service. It is called start.bat

@ECHO off
START c:\Ruby193\bin\ruby c:\Ruby193\bin\thin start -R c:\coolapp\config.ru -p 4321 -a localhost -e production

My challenge is that this program only runs properly if it is "Run as Administrator" with admin privileges. So, I would like to add a line to check if this script is actually run with administrative privileges, and only execute if it is being run as administrator.

How can I do that from within the script?

Jay Godse
  • 14,293
  • 15
  • 80
  • 124
  • Looks like http://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights – marceljg Jun 03 '13 at 18:27
  • Possible duplicate of [Batch script: how to check for admin rights](http://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights) – and31415 Jan 23 '14 at 21:22

3 Answers3

3

Something like this might be what you need:

set isadmin=0
whoami /all | findstr /c:" S-1-16-12288 ">nul && set isadmin=1

That should result in the %isadmin% variable being either 1 or 0 depending on whether the shell was run as administrator or not.

This assumes the existance of the whoami utility which won't necessarily be available on older versions of Windows - I believe it was included from Windows Vista onwards though.

James Holderness
  • 21,596
  • 2
  • 35
  • 49
1

Two options:

  1. Provoke elevation from a WSH script, like documented in the blog post Scripting Elevation on Vista.

  2. Use an external executable that provokes the UAC prompt, such as Elevate32.exe/Elevate64.exe.

For your scenario, #2 may be preferable because you can detect whether the elevation prompt was canceled (exit code 1223) and you can also wait for the launched executable to finish before continuing (-w parameter).

Bill

Bill_Stewart
  • 18,984
  • 4
  • 42
  • 53
0

It would probably be easier to convert the script to VBScript, then you can more easily check for Admin privileges and even elevate the script to Admin.

See here for how to do the check in VBScript: VBScript: Check if the script has administrative permissions

Community
  • 1
  • 1
canhazbits
  • 1,609
  • 1
  • 13
  • 19