Public/Get-LocalAdministrator.ps1
|
function Get-LocalAdministrator { <# .SYNOPSIS Lists the members of the local Administrators group. .DESCRIPTION Enumerates all members of the local Administrators group, including users and groups, along with their object class and principal source (local, Active Directory, etc.). Returns one object per member. .INPUTS None. Parameters must be supplied directly. .OUTPUTS System.Management.Automation.PSCustomObject .PARAMETER ComputerName The target computer. Defaults to the local machine. .EXAMPLE Get-LocalAdministrator Lists all local Administrators group members on the local machine. .EXAMPLE Get-LocalAdministrator -ComputerName 'Workstation01' Lists all local Administrators group members on Workstation01. .EXAMPLE 'Workstation01','Workstation02' | ForEach-Object { Get-LocalAdministrator -ComputerName $_ } Lists administrators across multiple machines. .NOTES Read-only. Does not modify any system state. Requires Administrator privileges on the target machine. Remote operations require WinRM to be configured on the target machine. #> [CmdletBinding()] [OutputType([System.Management.Automation.PSCustomObject])] param ( [Parameter(Mandatory = $false)] [string]$ComputerName = $env:COMPUTERNAME ) $isLocal = ($ComputerName -ieq $env:COMPUTERNAME) -or ($ComputerName -ieq 'localhost') -or ($ComputerName -eq '127.0.0.1') $getMembers = { Get-LocalGroupMember -Group 'Administrators' -ErrorAction Stop | ForEach-Object { [PSCustomObject]@{ Name = $_.Name ObjectClass = $_.ObjectClass PrincipalSource = $_.PrincipalSource } } } if ($isLocal) { $members = & $getMembers } else { $members = Invoke-Command -ComputerName $ComputerName -ScriptBlock $getMembers } foreach ($member in $members) { [PSCustomObject]@{ ComputerName = $ComputerName Name = $member.Name ObjectClass = $member.ObjectClass PrincipalSource = $member.PrincipalSource } } } |