PoshCode Archive  Artifact Content

Artifact 8b4d5b88a716524d3fbc85fe796e657ab1002f467fd666c04c0c5e35a6fc9095:

  • File get-localadministrators.ps1 — part of check-in [8295bbfaef] at 2018-06-10 13:47:02 on branch trunk — retrieves local administrators of a maschine using System.DirectoryServices.AccountManagement via Well-known security identifiers (user: Carsten Kr size: 2746)

# encoding: utf-8
# api: powershell
# title: get-localadministrators
# description: retrieves local administrators of a maschine using System.DirectoryServices.AccountManagement via Well-known security identifiers
# version: 0.1
# type: class
# author: Carsten Kr
# license: CC0
# function: get-localadministrators
# x-poshcode-id: 4861
# x-archived: 2014-02-09T11:32:13
# x-published: 2014-01-30T18:08:00
#
#
<#

.NOTES

    Author: Carsten Kr├╝ger - cakruege+poshcode@gmail.com

#>

Add-Type @'
public class MyAccounts
{
    public System.Collections.ArrayList users; 
    public System.Collections.ArrayList groups;
}
'@   

function get-localadministrators {
    param ([string]$computername=$env:computername)

    $computername = $computername.toupper()
    
                Add-Type -AssemblyName System.DirectoryServices.AccountManagement
                $PrincipalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Machine, $computername)
                           
                $GroupPrincipal = New-Object System.DirectoryServices.AccountManagement.GroupPrincipal($PrincipalContext)
                $Searcher = New-Object System.DirectoryServices.AccountManagement.PrincipalSearcher
                $Searcher.QueryFilter = $GroupPrincipal
                $localadmins = $Searcher.FindAll() | where {$_.Sid -eq 'S-1-5-32-544'} # Administrators group
                                              
                $users = New-Object System.Collections.ArrayList
                $groups = New-Object System.Collections.ArrayList
                
                $objOutput= New-Object MyAccounts
                              
                foreach ($ladmin in $localadmins.Members)
                {                 
                     if ($ladmin.ContextType -eq 'Machine')
                     {
                           [void] $users.Add($ladmin.Context.Name+'\'+$ladmin.SamAccountName)
                     }                                        
                    if ($ladmin.StructuralObjectClass -eq 'user') {
                      [void] $users.Add($ladmin.Context.Name+'\'+$ladmin.SamAccountName)
                    }                  
                    if ($ladmin.StructuralObjectClass -eq 'group') {
                      [void] $groups.Add($ladmin.Context.Name+'\'+$ladmin.SamAccountName)
                    }                                        
                }    
                
                $objOutput.users=$users
                $objOutput.groups=$groups
                
                    
    return $objoutput
}#end function