Skip to content

Instantly share code, notes, and snippets.

@klauern
Forked from charlesroper/gist:65931
Created July 25, 2009 04:06
Show Gist options
  • Save klauern/154687 to your computer and use it in GitHub Desktop.
Save klauern/154687 to your computer and use it in GitHub Desktop.
Method to detect whether we are running from an elevated command-prompt under Vista/Win7 or Administrator in WinXP
#
# Method to detect whether we are running from an elevated command-prompt
# under Vista/Win7 or as part of the local Administrators group in WinXP.
#
def elevated?
whoami = `whoami /groups` rescue nil
if whoami =~ /S-1-16-12288/
true
else
admin = `net localgroup administrators | find "%USERNAME%"` rescue ""
if admin.empty?
false
else
true
end
end
end
#
# A more terse version of the same thing.
#
def elevated?
whoami = `whoami /groups` rescue nil
if whoami =~ /S-1-16-12288/
true
else
admin = `net localgroup administrators | find "%USERNAME%"` rescue ""
admin.empty? ? false : true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment