-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuserCount.ps1
276 lines (233 loc) · 10.2 KB
/
userCount.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<#
.SYNOPSIS
[Incomplete script!] This is a template for new scripts
.DESCRIPTION
[Incomplete script!] This is a template for new scripts
.EXAMPLE
C:\PS>.\template.ps1
This will bring up the new script
.INPUTS
There are no inputs that can be directed to this script
.OUTPUTS
All outputs are sent to the console and logged in the log folder
.NOTES
Author: Robert Weber
Date: Nov 19, 2015
#>
[CmdletBinding()]
param (
[string]$ou = "LDAP://",
[string]$cOu = "",
[string]$uOu = ""
)
clear
$error.clear()
. .\lib\PSClass.ps1
(gci .\lib) | % { . "$($_.FullName)" }
$userCountClass = new-PSClass userCount{
note -static PsScriptName "userCount"
note -static Description ( ($(((get-help .\userCount.ps1).Description)) | select Text).Text)
note mainProgressBar
note -private startTime (get-date)
note -private exporter $ExportClass.New()
note -private cOu ""
note -private uOu ""
note -private domainusers @()
note -private exceptionUsers @()
note -private disabledUsers @()
note -private offlineSystems @()
note -private localSystems @()
note -private domain "$( ([ADSI]'LDAP://RootDSE').Get('rootDomainNamingContext') )"
note -private adRoot ""
note -private objUserSearch ""
constructor{
param()
if($uOu -ne '' -and $cOu -ne ''){
$private.cou = $cou
$private.uou = $uou
}else{
$private.cou = $ou
$private.uou = $ou
}
$private.adRoot = ([adsi]$private.cou)
$private.objUserSearch = New-Object System.DirectoryServices.DirectorySearcher($private.adRoot)
}
method -private getLocalUsers{
$uiClass.writeColor( "$($uiClass.STAT_WAIT) Parsing Local Users" )
$localPBar = $progressBarClass.New( @{ "parentId" = 1; "Activity" = "0 / 0 : Parsing Local Users"; "Status" = ("{0:N2}% complete" -f 0); "PercentComplete" = 0; "Completed" = $false; "id" = 2 }).Render()
#local admin groups
$ds = New-Object DirectoryServices.DirectorySearcher
$ds.Filter = "ObjectCategory=Computer"
$ds.SearchRoot = $private.cou
$systems = $ds.FindAll()
$totalSys = $systems.count
$currentSys = 1
foreach($system in ( $systems | sort Path) ){
$hostName = $($system.properties.item('cn'))
$uiClass.writeColor( "$($uiClass.STAT_OK) Connecting to Host #green#$($hostName)#" )
$i = (100*($currentSys / $totalSys))
$runtime = [string]::format("{0:N2} sec(s)", ($(get-date) - $private.startTime).totalSeconds)
$timePer = [string]::format("{0:N2} sec(s)",( (($(get-date) - $private.startTime).totalSeconds)/$currentSys) )
$eta = [string]::format("{0:N2} sec(s)",( (($(get-date) - $private.startTime).totalSeconds)/$currentSys)*$totalSys - ($(get-date) - $private.startTime).totalSeconds )
$localPBar.Activity("$currentSys / $totalSys : $($hostName) ").Status("{0:N2}% complete; Time Per Host $timePer, Total RunTime $runtime; ETA $eta" -f $i).Percent($i).Render()
$ou = ((($system.properties.item('adspath') -replace 'LDAP://','' -split ',') | ? { $_ -like 'OU=*' } | % { $_ -replace 'OU=','' }))
[array]::Reverse($ou)
$Result = Get-WmiObject -Class win32_pingstatus -Filter "address='$($hostName.Trim())'"
if( ($Result.StatusCode -eq 0 -or $Result.StatusCode -eq $null ) -and $utilities.isBlank($Result.IPV4Address) -eq $false ) {
try{
([adsi]"WinNT://$hostName,computer").psbase.children | ? { $_.psbase.schemaClassName -eq 'group'} | % {
$groupName = $($_.path.substring( $_.path.lastIndexOf('/')+1))
$Group = [ADSI]"$($_.path),group"
[Array]$MemberNames = @($Group.psbase.Invoke("Members")) | % { $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) } | Sort
if($MemberNames-ne $null){
$private.localSystems += New-Object PSObject -Property @{
OU = $ou -join '\'
Host = $hostName.ToLower()
Group = $groupName
Members = ($MemberNames -join ";")
}
}
}
}catch{
$private.offlineSystems += new-object PSObject -Property @{
OU = $ou -join '\'
Host = $hostName.ToLower()
}
$uiClass.writeColor( "$($uiClass.STAT_ERROR) `tError Parsing Host #green#$($hostName)#" )
}
}else{
$private.offlineSystems += new-object PSObject -Property @{
OU = $ou -join '\';
Host = $hostName.ToLower()
}
$uiClass.writeColor( "$($uiClass.STAT_ERROR) `tHost #green#$($hostName)# is #yellow#Offline#" )
}
$currentSys++
}
$localPBar.Completed($true).Render()
}
method -private getADUsers{
$uiClass.writeColor( "$($uiClass.STAT_WAIT) Parsing Domain Users" )
$private.objUserSearch.filter = "(&(objectCategory=person)(objectClass=user))"
$private.domainusers = $private.objUserSearch.findall() | select *, @{e={[string]$adspath=$_.properties.adspath;$account=[ADSI]$adspath;$account.psbase.invokeget('AccountDisabled')};n='Disabled'} | sort Path
$private.objUserSearch.filter = "(&(objectCategory=person)(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=262144)))"
$private.objUserSearch.findall() | % {
$private.exceptionUsers += $_.properties.item('cn')
}
$private.objUserSearch.filter = "(&(objectCategory=person)(objectClass=user)((userAccountControl:1.2.840.113556.1.4.803:=2)))"
$private.objUserSearch.findall() | % {
$private.disabledUsers += $_.properties.item('cn')
}
}
method -private exportADUsers{
$uiClass.writeColor( "$($uiClass.STAT_WAIT) Exporting Domain Users" )
$list = @()
foreach($domainUser in $private.domainusers){
$userName = $($domainUser.properties.item('cn'))
if( (([ADSISEARCHER]"samaccountname=$($userName)").Findone().Properties.memberof -replace '^CN=([^,]+).+$','$1' | ? { $_ -like '*admin*'}) -ne $null ){
$adminPriv = 'X'
}else{
$adminPriv = ''
}
$list += (New-Object PSObject -Property @{
UserName = $userName;
AdminPriv = $adminPriv;
Exception = @('','X')[$private.exceptionUsers -contains $userName];
Disabled = @('','X')[$private.disabledUsers -contains $userName];
})
}
# $list | select Username, AdminPriv, Exception, Disabled | ft | out-string | write-host
$private.exporter.addWorkSheet('Domain Accounts')
$colHeaders = @("Username","Admin Privileges","Exception List","Disabled")
$col = 1
$colHeaders | %{
$private.exporter.updateCell(1,$col,$_)
$private.exporter.formatCell(1,$col,[export.excelStyle]::Header)
$col = $col + 1
}
$row = 2
$list | % {
$private.exporter.addRow($row, @($_.userName, $_.AdminPriv, $_.Exception, $_.Disabled) )
$row++
}
$private.exporter.autoFilterWorksheet(1)
$private.exporter.autofitColumns()
$uiClass.writeColor( "$($uiClass.STAT_WAIT) Exporting Domain User Summary" )
$private.exporter.addWorkSheet('Domain Account Summary')
$colHeaders = @("Statistic","Count")
$col = 1
$colHeaders | %{
$private.exporter.updateCell(1,$col,$_)
$private.exporter.formatCell(1,$col,[export.excelStyle]::Header)
$col = $col + 1
}
$private.exporter.addRow(2, @("Domain Users in B:", $list.count ) )
$private.exporter.addRow(3, @("Domain Users in B with Exceptions:", $(($list | ? { $_.Exception -eq 'X' }).count) ) )
$private.exporter.addRow(4, @("Domain Users in B that are not disabled:", $(($list | ? { $_.Disabled -ne 'X' }).count) ) )
$private.exporter.addRow(5, @("Domain Users in B that are disabled:", $(($list | ? { $_.Disabled -eq 'X' }).count) ) )
$private.exporter.addRow(6, @("Domain Users in B that are not disabled and on the exceptions list:", $(($list | ? { $_.Disabled -ne 'X' -and $_.Exception -eq 'X' }).count) ) )
$private.exporter.addRow(7, @("Domain Users in B that are disabled on the exceptions list:", $(($list | ? { $_.Disabled -eq 'X' -and $_.Exception -eq 'X' }).count) ) )
$private.exporter.addRow(8, @("Domain Users in B that are privileged:", $(($list | ? { $_.AdminPriv -eq 'X' }).count) ) )
$private.exporter.addRow(9, @("Domain Users in B that are privileged on exceptions list:", $(($list | ? { $_.AdminPriv -eq 'X' -and $_.Exception -eq 'X' }).count) ) )
$private.exporter.autoFilterWorksheet(1)
$private.exporter.autofitColumns()
}
method -private exportLocalUsers{
$uiClass.writeColor( "$($uiClass.STAT_WAIT) Exporting Local Users" )
$private.exporter.addWorkSheet('Local Accounts')
$colHeaders = @("OU","Host","Group","Members")
$col = 1
$colHeaders | %{
$private.exporter.updateCell(1,$col,$_)
$private.exporter.formatCell(1,$col,[export.excelStyle]::Header)
$col = $col + 1
}
$row = 2
foreach($system in ( $private.localSystems | sort OU, Host) ){
foreach($member in ($system.Members -split ';')){
$private.exporter.addRow($row, @($system.ou, $system.Host, $system.Group, $member) )
$row++
}
}
$private.exporter.autoFilterWorksheet(1)
$private.exporter.autofitColumns()
}
method -private exportOfflineSystems{
$uiClass.writeColor( "$($uiClass.STAT_WAIT) Exporting Offline Systems" )
$private.exporter.addWorkSheet('Offline Systems')
$colHeaders = @("OU","Hostname")
$col = 1
$colHeaders | %{
$private.exporter.updateCell(1,$col,$_)
$private.exporter.formatCell(1,$col,[export.excelStyle]::Header)
$col = $col + 1
}
$row = 2
$private.offlineSystems | sort OU,Host | % {
$private.exporter.addRow($row, @($_.OU, $_.Host) )
$row++
}
$private.exporter.autoFilterWorksheet(1)
$private.exporter.autofitColumns()
}
method -private export{
$uiClass.writeColor( "$($uiClass.STAT_WAIT) Exporting Results" )
$private.exportADUsers()
$private.exportLocalUsers()
$private.exportOfflineSystems()
$ts = (get-date -format "yyyyMMddHHmmss")
$private.exporter.saveAs([System.IO.Path]::GetFullPath("$($pwd.ProviderPath)\results\$($userCountClass.PsScriptName)_$ts.xml"))
}
method Execute{
param()
$uiClass.writeColor( "$($uiClass.STAT_WAIT) Executing Scan" )
$this.mainProgressBar = $progressBarClass.New( @{ "parentId" = 0; "Activity" = "1 / 3 : Parsing Active Directory Users"; "Status" = ("{0:N2}% complete" -f 10); "PercentComplete" = 10; "Completed" = $false; "id" = 1 }).Render()
$private.getADUsers()
$this.mainProgressBar.Activity("Parsing Local Users").Status("{0:N2}% complete" -f 50).Percent(50).Render()
$private.getLocalUsers()
$this.mainProgressBar.Activity("Exporting Results").Status("{0:N2}% complete" -f 75).Percent(75).Render()
$private.export()
$uiClass.errorLog()
}
}
$userCountClass.New().Execute() | out-null