forked from joerod/powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_admin_users.ps1
80 lines (64 loc) · 2.79 KB
/
local_admin_users.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
#Gets list of machines from specified OU
Function Get-CompList{
Get-ADObject -Filter { ObjectClass -eq "computer" } -SearchBase "OU=Resources,DC=NWTraders,DC=LOCAL" `
| Select-Object -expandproperty Name |Sort
}
<#
Gets a list of local Admin accounts from each computers in OU from Get-Complist function, will ping machine
to see if its alive and write error message if machine is unavalible
#>
Function Get-AdminGroups{
foreach($i in Get-CompList){
if (-not (Test-Connection -computername $i -count 1 -Quiet -ErrorAction SilentlyContinue)) {
write-host $i.toupper() "is Unavalible" -foreground red
"`r"
}
else {
Write-host "Added $i to list...."
$adsi = [ADSI]"WinNT://$i"
$Object = $adsi.Children | ? {$_.SchemaClassName -eq 'user'} | % {
$UserName = $_.Name -join '';
New-Object -TypeName PSCustomObject -Property @{
ComputerName = $i.toupper() -join ''
UserName = $UserName
Groups = ($_.Groups() |Foreach-Object {$_.GetType().InvokeMember("Name",'GetProperty', $null, $_, $null)}) -join ','
Disabled = (Get-WmiObject -ComputerName $i -Class Win32_UserAccount -Filter "LocalAccount='$true' and name='$UserName'"`
|Select-Object -expandproperty Disabled) -join ''
}
}
$Object | Select-object ComputerName,UserName,Groups,Disabled |? {$_.Groups -match "Administrators*"}
"`r"
}
}
}
$admins = Get-AdminGroups -ErrorAction SilentlyContinue
#built-in admin account not named "winroot" will be changed via group policy
Function Remove-Admin{
foreach($admin in $admins){
#renames a local account named winroot that is not built-in then disables it. This is done so our GPO
#will can rename the built-in admin account to winroot.
if($admin.UserName -match "winroot" -and $admin.groups -match "Administrators,Users"){
$user = [ADSI]("WinNT://" + $($admin.computername) + "/$($admin.UserName),user")
$user.psbase.rename("winroot_old")
$user = [ADSI]("WinNT://" + $($admin.computername) + "/winroot_old")
$user.UserFlags[0] = $User.UserFlags[0] -bor 0x2
$user.SetInfo()
Write-host $($admin.computername) "has been renamed to winroot_old"
}
#sets account password and disables all local accounts
if($admin.UserName -notmatch "winroot"){
$user = [ADSI]("WinNT://" + $($admin.computername) + "/" + $($admin.UserName))
$user.UserFlags[0] = $User.UserFlags[0] -bor 0x2
$user.SetInfo()
Write-host "\\$($admin.computername)\$($admin.UserName) has been disabled `r"
}
#enables winroot built-in account if its disabled
if($admin.UserName -match "winroot" -and $admin.groups -match "Administrators"){
$user = [ADSI]("WinNT://" + $($admin.computername) + "/winroot")
$user.UserFlags[0] = $User.UserFlags[0] -bxor 0x2
$user.SetInfo()
Write-host "\\$($admin.computername)\winroot has been enabled"
}
}
}
Remove-Admin -ErrorAction SilentlyContinue