-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasswordQuality-Create-Report.ps1
209 lines (176 loc) · 10.4 KB
/
PasswordQuality-Create-Report.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
# 04.12.2023 - Create_PasswordQuality_Report.ps1 by https://github.com/CamFlyerCH
# Init
Import-Module ImportExcel
$ScriptPath = $MyInvocation.MyCommand.Path
$ScriptDir = Split-Path -Parent $ScriptPath
$DaysOld = 365
$DataFiles = Get-ChildItem -Path $ScriptDir -Filter "PasswordQuality_*_PWQ-Data.xml"
ForEach($DataFile in $DataFiles){
$Domain = ($DataFile.BaseName.Replace("PasswordQuality_","")).Replace("_PWQ-Data","")
Write-Host ("Start working on domain $Domain")
$QualityData = Import-Clixml -LiteralPath $DataFile.FullName
$AccountData = Import-Clixml -Path ($ScriptDir + "\PasswordQuality_" + $Domain + "_Accounts.xml")
Write-Host ("Imported quality data and " + $AccountData.Count + " accounts")
# Create Excel
$ExcelFilePath = $ScriptDir + "\PasswordQuality_" + $Domain + "_Report_" + $(Get-Date $DataFile.LastWriteTime -Format "yyyy.MM.dd_HH.mm") + ".xlsx"
If($ExcelFilePath){
If (Test-Path $ExcelFilePath) { Remove-Item $ExcelFilePath -Force }
$ExcelPack = Open-ExcelPackage -Path $ExcelFilePath -Create
Write-Host("Excel package defined : $ExcelFilePath")
}
ForEach($Property in $QualityData.PSObject.Properties.Name) {
# Special for accounts with the same password hash (else case below)
If($Property -ne "DuplicatePasswordGroups"){
Write-Host ("Working on " + $QualityData.$Property.Count + " accounts with $Property")
$AccountList = @()
ForEach($User in $QualityData.$Property){
$ShortUser = $User.Split("\")[1]
ForEach($Account in $AccountData){
If($Account.sAMAccountName -eq $ShortUser){
$AccountList += $Account
Break
} # End if account found in account data
} # End for each account in all account data
} # End for each account of this quality type
# Output to additional Excel sheet
$LengthStyle = {
param(
$workSheet,
$totalRows,
$lastColumn
)
2..$totalRows | ForEach-Object{
# Mark old dates
$Type = $WorkSheet.Cells["M$($_)"]
$PWNeverExp = $WorkSheet.Cells["F$($_)"]
$Created = $WorkSheet.Cells["B$($_)"]
# Coloring for LastLogon
$Cell = $WorkSheet.Cells["D$($_)"]
If(($Cell.Value -AND $Type.Value -eq "Computer") -or ($Cell.Value -AND $PWNeverExp.Value -eq "TRUE")){
$TimeDiff = New-TimeSpan -Start $Cell.Value -End $DataFile.LastWriteTime
If ($TimeDiff.TotalDays -gt $DaysOld){
$Cell.Style.Fill.PatternType='Solid'
$Cell.Style.Fill.BackgroundColor.SetColor('LightPink')
}
}
If([string]::IsNullOrEmpty($Cell.Value) -AND $PWNeverExp.Value -eq "TRUE"){
$TimeDiff = New-TimeSpan -Start $Created.Value -End $DataFile.LastWriteTime
If ($TimeDiff.TotalDays -gt $DaysOld){
$Cell.Style.Fill.PatternType='Solid'
$Cell.Style.Fill.BackgroundColor.SetColor('LightGoldenrodYellow')
}
}
# Coloring PwLastSet
$Cell=$WorkSheet.Cells["E$($_)"]
If($Cell.Value -AND $PWNeverExp.Value -ne "TRUE"){
$TimeDiff = New-TimeSpan -Start $Cell.Value -End $DataFile.LastWriteTime
If ($TimeDiff.TotalDays -gt $DaysOld){
$Cell.Style.Fill.PatternType='Solid'
$Cell.Style.Fill.BackgroundColor.SetColor('LightGoldenrodYellow')
}
}
If($Cell.Value){
$TimeDiff = New-TimeSpan -Start $Cell.Value -End $DataFile.LastWriteTime
If ($TimeDiff.TotalDays -gt ($DaysOld * 10)){
$Cell.Style.Fill.PatternType='Solid'
$Cell.Style.Fill.BackgroundColor.SetColor('LightPink')
}
}
}
}
If($Script:ExcelPack -AND $AccountList.Count -gt 0){
# Add details to Excel
$Script:ExcelPack = $AccountList | Export-Excel -ExcelPackage $Script:ExcelPack -PassThru -WorksheetName $Property -BoldTopRow -AutoFilter -FreezeTopRowFirstColumn -AutoSize -CellStyleSB $LengthStyle
$Script:ExcelPack.$Property.View.ZoomScale = 80
$Columns = 2..5
ForEach($Column in $Columns){
Set-ExcelColumn -ExcelPackage $Script:ExcelPack -Worksheetname $Property -Column $Column -VerticalAlignment Top -HorizontalAlignment Left -Width ([Int]($Script:ExcelPack.$Property.Column($Column).Width * 1.3))
}
$Columns = 7..($Script:ExcelPack.$Property.Dimension.Columns)
ForEach($Column in $Columns){
Set-ExcelColumn -ExcelPackage $Script:ExcelPack -Worksheetname $Property -Column $Column -VerticalAlignment Top -HorizontalAlignment Left -Width ([Int]($Script:ExcelPack.$Property.Column($Column).Width * 1.1))
}
}
} Else {
$PasswordGroupsUsers = [ordered] @{}
$Count = 0
ForEach($Group in $QualityData.$Property) {
$Count++
Write-Host ("Working on " + $QualityData.$Property.Count + " groups with " + $Group.Count + " accounts in group $Count with $Property")
ForEach($User in $Group) {
#Write-Host $User
$PasswordGroupsUsers[$User] = "Group $Count"
$ShortUser = $User.Split("\")[1]
ForEach($Account in $AccountData){
If($Account.sAMAccountName -eq $ShortUser){
$AccountList += $Account | Select-Object @{n="Group";e={$Count}},*
Break
} # End if account found in account data
} # End for each account in all account data
} # End for each account in this group of DuplicatePasswordGroups
} # End for each group of DuplicatePasswordGroups
# Output to additional Excel sheet
$LengthStyle = {
param(
$workSheet,
$totalRows,
$lastColumn
)
2..$totalRows | ForEach-Object{
# Mark old dates
$Type = $WorkSheet.Cells["N$($_)"]
$PWNeverExp = $WorkSheet.Cells["G$($_)"]
$Created = $WorkSheet.Cells["C$($_)"]
# Coloring for LastLogon
$Cell = $WorkSheet.Cells["E$($_)"]
If(($Cell.Value -AND $Type.Value -eq "Computer") -or ($Cell.Value -AND $PWNeverExp.Value -eq "TRUE")){
$TimeDiff = New-TimeSpan -Start $Cell.Value -End $DataFile.LastWriteTime
If ($TimeDiff.TotalDays -gt $DaysOld){
$Cell.Style.Fill.PatternType='Solid'
$Cell.Style.Fill.BackgroundColor.SetColor('LightPink')
}
}
If([string]::IsNullOrEmpty($Cell.Value) -AND $PWNeverExp.Value -eq "TRUE"){
$TimeDiff = New-TimeSpan -Start $Created.Value -End $DataFile.LastWriteTime
If ($TimeDiff.TotalDays -gt $DaysOld){
$Cell.Style.Fill.PatternType='Solid'
$Cell.Style.Fill.BackgroundColor.SetColor('LightGoldenrodYellow')
}
}
# Coloring PwLastSet
$Cell=$WorkSheet.Cells["F$($_)"]
If($Cell.Value -AND $PWNeverExp.Value -ne "TRUE"){
$TimeDiff = New-TimeSpan -Start $Cell.Value -End $DataFile.LastWriteTime
If ($TimeDiff.TotalDays -gt $DaysOld){
$Cell.Style.Fill.PatternType='Solid'
$Cell.Style.Fill.BackgroundColor.SetColor('LightGoldenrodYellow')
}
}
If($Cell.Value){
$TimeDiff = New-TimeSpan -Start $Cell.Value -End $DataFile.LastWriteTime
If ($TimeDiff.TotalDays -gt ($DaysOld * 10)){
$Cell.Style.Fill.PatternType='Solid'
$Cell.Style.Fill.BackgroundColor.SetColor('LightPink')
}
}
}
}
If($Script:ExcelPack -AND $AccountList.Count -gt 0){
# Add details to Excel
$Script:ExcelPack = $AccountList | Export-Excel -ExcelPackage $Script:ExcelPack -PassThru -WorksheetName $Property -BoldTopRow -AutoFilter -FreezePane 2,3 -AutoSize -CellStyleSB $LengthStyle
$Script:ExcelPack.$Property.View.ZoomScale = 80
$Columns = 3..6
ForEach($Column in $Columns){
Set-ExcelColumn -ExcelPackage $Script:ExcelPack -Worksheetname $Property -Column $Column -VerticalAlignment Top -HorizontalAlignment Left -Width ([Int]($Script:ExcelPack.$Property.Column($Column).Width * 1.3))
}
$Columns = 8..($Script:ExcelPack.$Property.Dimension.Columns)
ForEach($Column in $Columns){
Set-ExcelColumn -ExcelPackage $Script:ExcelPack -Worksheetname $Property -Column $Column -VerticalAlignment Top -HorizontalAlignment Left -Width ([Int]($Script:ExcelPack.$Property.Column($Column).Width * 1.1))
}
}
} # End if DuplicatePasswordGroups
} # End for each quality type
# Close Excel
Close-ExcelPackage -ExcelPackage $ExcelPack -Show
Write-Host("Excel file saved : $ExcelFilePath")
} # End for each domain file