forked from joerod/powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_local_admins.ps1
34 lines (30 loc) · 1.2 KB
/
find_local_admins.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#this script will find all machines in an OU then show who is in the local administrators group.
Function Get-Complist{
Get-ADObject -Filter { ObjectClass -eq "computer" } -SearchBase "OU=Workstations,DC=Contoso,DC=LOCAL"|
Select-Object -expandproperty Name |Sort
}
Function find_local_admin{
foreach($i in Get-Complist){
#test if machine is on the network
if (!(Test-Connection -computername $i -count 1 -Quiet -ErrorAction SilentlyContinue)) {
Write-Warning "$i is Unavailable (Not Pingable)" | tee-object -filepath c:\
Continue
}
try{
invoke-command {
$members = net localgroup administrators |?{$_ -AND $_ -notmatch "command completed successfully"} | select -skip 4
$Results = @()
ForEach($member in $members){
New-Object PSObject -Property @{
Computername = $env:COMPUTERNAME
Users=$member
}
}
} -computer $i -HideComputerName | Select * -ExcludeProperty RunspaceID
}
catch{
Write-Warning "$i - Cannot WinRM"
}
}
}
find_local_admin | select * -ExcludeProperty PSComputerName,PSShowComputerName |Export-Csv C:\Users\joerod\Desktop\admins.csv -NoTypeInformation -Append