I was reviewing some PowerShell code for a colleague the other day when I stumbled across some code he’d found on the web that I immediately stole, re-wrote into a function, and made use of. Although this information is out there in the PowerShell community and blogosphere I thought I’d post the code, reformatted into a function for dot sourcing, if for no other reason than for me to be able to look it up when I’m on customer site. J
## snipped other utility methods ## ################################################################ # Boolean CheckSessionIsElevated() # # Check whether or not the current identity (the principal # running the current PS session) is a member of # Builtin\Administrators. # # Returns true if the current principal is a member of # administrators; false otherwise # # Function based on the code at: # http://www.interact-sw.co.uk/iangblog/2007/02/09/pshdetectelevation ################################################################ function CheckSessionIsElevated { [System.Security.Principal.WindowsPrincipal]$currentPrincipal = ` New-Object System.Security.Principal.WindowsPrincipal( [System.Security.Principal.WindowsIdentity]::GetCurrent()); [System.Security.Principal.WindowsBuiltInRole]$administratorsRole = ` [System.Security.Principal.WindowsBuiltInRole]::Administrator; if($currentPrincipal.IsInRole($administratorsRole)) { return $true; } else { return $false; } }
As you can see, I’ve taken what Ian Griffiths posted and formatted it the way I like it and added it to my own function library (that probably should be a module). J
Advertisements