PoshCode Archive  Artifact Content

Artifact 04defe83bb2415e04e301b9df9563fcfeedd987e71235fc05059ef048746cb18:

  • File is-local-admin.ps1 — part of check-in [d6916d0c8c] at 2018-06-10 13:52:55 on branch trunk — I could not find a solution that checks if a user is in local admin groups that can handle a situation if the user is there indirectly, that is a member of a group that is a part of the admin group. The below is what I came up with. The $env:USERDOMAIN part is iffy, since it depends on the account the script is run under, but that was enough for my purposes. Also it seems to detect the membership correctly even for local users and for users that are not local and not in the $env:USERDOMAIN domain, which is good (user: zespri size: 1325)

# encoding: ascii
# api: powershell
# title: is local admin
# description: I could not find a solution that checks if a user is in local admin groups that can handle a situation if the user is there indirectly, that is a member of a group that is a part of the admin group. The below is what I came up with. The $env:USERDOMAIN part is iffy, since it depends on the account the script is run under, but that was enough for my purposes. Also it seems to detect the membership correctly even for local users and for users that are not local and not in the $env:USERDOMAIN domain, which is good
# version: 0.1
# author: zespri
# license: CC0
# x-poshcode-id: 5273
# x-archived: 2014-08-29T21:07:08
# x-published: 2014-07-02T01:19:00
#
#
#this is the account
$accountName = "BLA\user"

#this cruft here is so that we get UPN for the WindowsIdentity conscturctor
add-type -AssemblyName System.DirectoryServices.AccountManagement
$pc = new-object System.DirectoryServices.AccountManagement.PrincipalContext Domain,$env:USERDOMAIN
$p = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($pc,$accountName)

#and finally
$wi = new-object System.Security.Principal.WindowsIdentity $p.UserPrincipalName
$wp = [System.Security.Principal.WindowsPrincipal]$wi
$wp.IsInRole("Administrators")