Skip to content

Instantly share code, notes, and snippets.

@vScripter
Created April 9, 2014 21:29
Show Gist options
  • Save vScripter/10318913 to your computer and use it in GitHub Desktop.
Save vScripter/10318913 to your computer and use it in GitHub Desktop.
Get Members of the local Administrators group on Selected Computers/Servers
[cmdletbinding()]
param (
[parameter(mandatory = $true)]
[string[]]$Servers
)
$results = @()
$Log = "C:\Invoke-LocalAdminAudit.ps1.log"
if (Test-Path $Log) { Remove-Item $Log -Force }
Write-Verbose "Gathering Local Admin Membership..."
ForEach ($server in $servers)
{
$Report = "C:\LocalAdminReport.csv"
if (Test-Path $Report) { Remove-Item -Path $Report -Force }
if (Test-Connection $server -Count 2 -Quiet)
{
# Setup empty array and assign to a variable
$admins = @()
# assign ADSI local admin query to a variable
$group = [ADSI]"WinNT://$server/Administrators"
# Call members from the group
$members = @($group.psbase.Invoke("Members"))
# Cycle through each member and add the member name to a custom PSObject
$members | ForEach {
$obj = New-Object psobject -Property @{
Server = $Server
Admin = $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
}# end $obj
$admins += $obj
}# end foreach
# Store the results in the 'results' array
$results += $admins
}# end if
else
{
}# end else
}# end foreach
# export the results
$results | Export-Csv $Report -Append -NoTypeInformation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment