forked from gangstanthony/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-LocalAdmin.ps1
157 lines (136 loc) · 6.27 KB
/
Get-LocalAdmin.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# wmi
# https://gallery.technet.microsoft.com/scriptcenter/Get-remote-machine-members-bc5faa57
# adsi
# https://social.technet.microsoft.com/Forums/windowsserver/en-US/7bf4e490-0f3f-4a33-8350-2e78a869f1ed/list-remote-local-admins?forum=winserverpowershell
# https://www.reddit.com/r/PowerShell/comments/49oemk/how_to_display_only_those_lines_that_contain/
# https://www.petri.com/use-powershell-to-find-local-groups-and-members
# https://learn-powershell.net/2013/08/11/get-all-members-of-a-local-group-using-powershell/
# adsi fix
# http://stackoverflow.com/questions/31949541/print-local-group-members-in-powershell-5-0
# AccountManagement method
# http://stackoverflow.com/questions/30202452/list-all-local-administrator-accounts-excluding-domain-admin-and-local-admin
# http://blogs.msmvps.com/richardsiddaway/2009/04/21/user-module-local-account/
# https://github.com/lazywinadmin/PowerShell/blob/master/TOOL-Get-LocalGroupMember/Get-LocalGroupMember.ps1
# examples
# https://www.reddit.com/r/PowerShell/comments/4hmpj6/organising_outfile/d2rfrbs
# change local admin pw
# https://community.spiceworks.com/scripts/show/2978-change-local-user-password-for-remote-computers
# $user = [adsi]"WinNT://$computer/administrator,user"
# $user.SetPassword($decodedpassword)
# $user.SetInfo()
<##### VALIDATE ADMIN CREDENTIALS
# https://www.reddit.com/r/PowerShell/comments/4i1q5w/how_can_i_run_this_static_method_on_a_remote/
$comp = $env:computername
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Machine, $comp)
write-host $ds.ConnectedServer
$DS.ValidateCredentials('Admin', "'")
#>
function Get-LocalAdmin {
param (
$comp = $env:COMPUTERNAME,
[ValidateSet('AccountManagement', 'ADSI', 'WMI')]
$method = 'ADSI',
[ValidateSet('Administrators', 'Remote Desktop Users')]
$groupname = 'Administrators'
)
if ($method -eq 'AccountManagement') {
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ctype = [System.DirectoryServices.AccountManagement.ContextType]::Machine
$idtype = [System.DirectoryServices.AccountManagement.IdentityType]::SamAccountName
$context = New-Object System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ctype, $comp
try{ $obj = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($context, $idtype, $groupname) }catch{ continue }
$obj.Members | % {
[pscustomobject]@{
Computer = $comp
Domain = $_.Context.Name
User = $_.samaccountname
}
}
} elseif ($method -eq 'ADSI') { # adsi
$group = [ADSI]"WinNT://$comp/$groupname"
$group.Invoke('Members') | % {
$path = ([adsi]$_).path
[pscustomobject]@{
Computer = $comp
Domain = $(Split-Path (Split-Path $path) -Leaf)
User = $(Split-Path $path -Leaf)
#User = $_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null) # gives error: Error while invoking GetType. Could not find member.
#User = $_.GetType.Invoke().InvokeMember('Name', 'GetProperty', $null, $_, $null) # works, but other way is shorter
#User = ([ADSI]$_).InvokeGet('Name') # works fine, I'm just no longer using this way
}
}
} elseif ($method -eq 'WMI') { # takes crazy long because it lists every group (even many domain groups) then finds the Administrators group
Get-WmiObject -Query 'SELECT GroupComponent, PartComponent FROM Win32_GroupUser' -ComputerName $comp | ? GroupComponent -Like "*`"$groupname`"" | % {
$_.partcomponent -match '\\(?<computer>[^\\]+)\\.+\.domain="(?<domain>.+)",name="(?<name>.+)"' | Out-Null
[pscustomobject]@{
Computer = $matches.computer
Domain = $matches.domain
User = $matches.name
}
}
}
}
<# adsi
$servers = Get-Content C:\Users\ExplainLikeImFly\Desktop\List.txt
$groups = 'Administrators', 'Remote Desktop Users'
$results = foreach ($server in $servers) {
foreach ($group in $groups) {
$obj = [ADSI]"WinNT://$server/$group"
$obj.Invoke('Members') | % {
$path = ([adsi]$_).path
[pscustomobject]@{
Computer = $server
Group = $group
Domain = $(Split-Path (Split-Path $path) -Leaf)
User = $(Split-Path $path -Leaf)
}
}
}
}
$results
#>
<# AccountManagement
$servers = Get-Content C:\Users\ExplainLikeImFly\Desktop\List.txt
$groups = 'Administrators', 'Remote Desktop Users'
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ctype = [System.DirectoryServices.AccountManagement.ContextType]::Machine
$idtype = [System.DirectoryServices.AccountManagement.IdentityType]::SamAccountName
$results = foreach ($server in $servers) {
$context = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ctype, $server
foreach ($group in $groups) {
try {
$obj = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($context, $idtype, $group)
}catch{
continue
}
$obj.Members | % {
[pscustomobject]@{
Computer = $server
Group = $group
Domain = $_.Context.Name
User = $_.samaccountname
}
}
}
}
$results
#>
<# wmi
$servers = Get-Content C:\Users\ExplainLikeImFly\Desktop\List.txt
$groups = 'Administrators', 'Remote Desktop Users'
$results = foreach ($server in $servers) {
foreach ($group in $groups) {
Get-WmiObject -Query 'SELECT GroupComponent, PartComponent FROM Win32_GroupUser' -ComputerName $server | ? GroupComponent -Like "*`"$group`"" | % {
$_.partcomponent -match '\\(?<computer>[^\\]+)\\.+\.domain="(?<domain>.+)",name="(?<name>.+)"' | Out-Null
[pscustomobject]@{
Computer = $matches.computer
Group = $group
Domain = $matches.domain
User = $matches.name
}
}
}
}
$results
#>