Welcome to MSDN Blogs Sign in | Join | Help

jaredpar's WebLog

Code, rants and ramblings of a programmer.

Syndication

News

Now Reading

Expert F#

What's a better book to read when learning F#?

Essential WPF

Thus far the best book I've read on WPF. Gets right down to working with WPF and the goals/history.

Purely Functional Data Structures

Reading this book makes me feel like I'm back in college. It will really get your mind going and is best read with a whiteboard handy.

Blog Roll

Eric Lippert
Dustin Campbell
Jon Skeet
Coding Horror
Brian McNamara
Brian Bondy
Hub FS
Full List

Monad: Detecting if you are an Admin

One of the LUA tasks I found very frustrating was detecting whether or not I was running as an Admin in a .BAT script.  It's very difficult to do this correctly and the best way I found was to write a separate program that would determine this for me.  This has a couple of limitation, the main one being that it adds extra dependencies to your scripts and makes them harder to deploy. 

Monad has greatly simplified this task as I can do the detection code uses simple scripts. 

# Determine if I am running as an Admin
function AmIAdmin()
{
 $local:ident = [System.Security.Principal.WindowsIdentity]::GetCurrent()
 
 foreach ( $local:groupIdent in $ident.Groups )
 {
  if ( $groupIdent.IsValidTargetType([System.Security.Principal.SecurityIdentifier]) )
  {
   $local:groupSid = $groupIdent.Translate([System.Security.Principal.SecurityIdentifier])
   if ( $groupSid.IsWellKnown("AccountAdministratorSid") -or $groupSid.IsWellKnown("BuiltinAdministratorsSid"))
   {
    return $true;
   }
  }
 }
 
 return $false;
}

I've added this to my profile so now it's really easy to determine if I can complete an action or need to prompt for credentials in my scrips. 

Published Monday, November 07, 2005 2:25 PM by Jared Parsons

Comments

# Detecting if you are an Admin @ Wednesday, August 01, 2007 11:56 AM

This came up on an internal alias. A customer wanted to know how to determine if there were running as

jaredpar's WebLog

New Comments to this post are disabled
Page view tracker