-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ps1
More file actions
290 lines (238 loc) · 10.7 KB
/
main.ps1
File metadata and controls
290 lines (238 loc) · 10.7 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Set manual script location if running in powershell ISE
If ([string]::IsNullOrEmpty($PSScriptRoot)) {
# Replace the location with your location of the root folder
$Global:scriptLocation = (Get-Item .).FullName
}
else {
$Global:scriptLocation = $PSScriptRoot
}
#######################################
######## Load all functions and assemblies
##########################
Import-Module "$Global:scriptLocation\root" -Verbose -Force
Set-Assemblies
[System.Reflection.Assembly]::LoadFrom("assembly\MahApps.Metro.dll")
#######################################
######## Variables
##########################
# Objects that hold user information for each tabItem
$Global:dbUser = @()
$Global:dbAdmin = @()
$Global:dbOffboarding = @()
# Password variables
[SecureString]$Global:pswdCurrent = $null
[SecureString]$Global:pswdNewOne = $null
[SecureString]$Global:pswdNewTwo = $null
# SyncHash needed to manipulate data out of runspaces
$Global:syncHash = [hashtable]::Synchronized(@{})
$Global:syncHash.activeRunspaces = 0
[System.Collections.ArrayList]$Global:syncHash.activeRunSpaceDomains = @()
# Load xaml
$XamlMainWindow = Set-XML("$Global:scriptLocation\resources\main.xaml")
$Reader = (New-Object System.Xml.XmlNodeReader $XamlMainWindow)
$Global:syncHash.Window = [Windows.Markup.XamlReader]::Load($Reader)
$Global:syncHash.Window.Title = "ADAM - Active Directory Account Manager"
# Grid for displaying results
$Global:userListView = $syncHash.Window.FindName("userListView")
$Global:adminListView = $syncHash.Window.FindName("adminListView")
$Global:offboardingListView = $syncHash.Window.FindName("offboardingListView")
# tab object for switching mode
$syncHash.tabAdmin = $syncHash.Window.FindName("tabAdmin")
$syncHash.statusBarText = $syncHash.Window.FindName("statusBarText")
$syncHash.statusBarProgress = $syncHash.Window.FindName("statusBarProgress")
$syncHash.statusBarBorder = $syncHash.Window.FindName("statusBarBorder")
# Put statusbar content in the right Z order
[System.Windows.Controls.Canvas]::SetZIndex($syncHash.statusBarBorder, 1)
[System.Windows.Controls.Canvas]::SetZIndex($syncHash.statusBarProgress, 2)
# Reset current active tab results
$btnResetSearch = $syncHash.Window.FindName("btnResetSearch")
$btnResetSearch.Add_Click(
{
Reset-App
})
$syncHash.userBoxDisable = $syncHash.Window.FindName("userBoxDisable")
$syncHash.editDB = $false
$syncHash.clipBoardRunspace = 0
$Global:tabControl = $syncHash.Window.FindName("tabControl")
$Global:btnFlyOut = $syncHash.Window.FindName("btnFlyOut")
$syncHash.flyOut = $syncHash.Window.FindName("flyOut")
# Get theme and accent buttons
$btnThemeWhite = $syncHash.Window.FindName("btnThemeBaseLight")
$btnThemeBlack = $syncHash.Window.FindName("btnThemeBaseDark")
$btnAccent = @()
$btnAccent += $syncHash.Window.FindName("btnAccentGreen")
$btnAccent += $syncHash.Window.FindName("btnAccentLime")
$btnAccent += $syncHash.Window.FindName("btnAccentCyan")
$btnAccent += $syncHash.Window.FindName("btnAccentCobalt")
$btnAccent += $syncHash.Window.FindName("btnAccentPurple")
$btnAccent += $syncHash.Window.FindName("btnAccentRed")
$btnAccent += $syncHash.Window.FindName("btnAccentOrange")
$btnAccent += $syncHash.Window.FindName("btnAccentYellow")
$btnAccent += $syncHash.Window.FindName("btnAccentBrown")
$btnAccent += $syncHash.Window.FindName("btnAccentSteel")
$btnAccent += $syncHash.Window.FindName("btnAccentMauve")
$btnAccent += $syncHash.Window.FindName("btnAccentSienna")
# Add color change function to all buttons
$btnAccent | ForEach-Object { $_.Add_Click( { Set-AccentColor $this }) }
$btnThemeWhite.Add_Click( { Set-ThemeSkin $this })
$btnThemeBlack.Add_Click( { Set-ThemeSkin $this })
#######################################
######## Password Change tab
##########################
$Global:btnSearchAccounts = $syncHash.Window.FindName("btnSearchAccounts")
$btnSearchAccounts.Add_Click(
{
Get-AllDomainAccounts
$btnSearchAccounts.IsEnabled = $false
$pwdBoxCur.IsEnabled = $true
$pwdBoxNew1.IsEnabled = $true
$pwdBoxNew2.IsEnabled = $true
$togglePwRnd.IsEnabled = $true
})
$Global:pwdBoxCur = $syncHash.Window.FindName("pwdBoxCur")
$pwdBoxCur.Add_PasswordChanged(
{
TextBoxPasswordHandler $_.KeyCode $pwdBoxCur.SecurePassword $pwdVerBtn
})
$pwdBoxCur.Add_KeyDown( {
param ($sender, $e)
if ($e.Key -eq 'Return' -or $e.Key -eq 'Enter') {
Test-AllCredentials
}
})
$Global:pwdVerBtn = $syncHash.Window.FindName("pwdVerBtn")
$pwdVerBtn.Add_Click(
{
Test-AllCredentials
})
$Global:pwdBoxNew1 = $syncHash.Window.FindName("pwdBoxNew1")
$Global:pwdBoxNew2 = $syncHash.Window.FindName("pwdBoxNew2")
$Global:setNewPswdBtn = $syncHash.Window.FindName("setNewPswdBtn")
$Global:btnCopyToClip = $syncHash.Window.FindName("btnCopyToClip")
$pwdBoxNew1.Add_PasswordChanged(
{
Set-SetNewPasswordButton -password1 $pwdBoxNew1.SecurePassword -password2 $pwdBoxNew2.SecurePassword -button $setNewPswdBtn
})
$pwdBoxNew2.Add_PasswordChanged(
{
Set-SetNewPasswordButton -password1 $pwdBoxNew1.SecurePassword -password2 $pwdBoxNew2.SecurePassword -button $setNewPswdBtn
})
$pwdBoxNew1.Add_KeyDown( {
param ($sender, $e)
if ($setNewPswdBtn.IsEnabled -eq $true -and ($e.Key -eq 'Return' -or $e.Key -eq 'Enter')) {
Set-AllCredentials $togglePwRnd $togglePwInd
}
})
$pwdBoxNew2.Add_KeyDown( {
param ($sender, $e)
if ($setNewPswdBtn.IsEnabled -eq $true -and ($e.Key -eq 'Return' -or $e.Key -eq 'Enter')) {
Set-AllCredentials $togglePwRnd $togglePwInd
}
})
$setNewPswdBtn.Add_Click(
{
Set-AllCredentials $togglePwRnd $togglePwInd
})
$btnCopyToClip.Add_Click(
{
Set-RunSpaceContentToClipBoard -password $Global:pwdBoxNew1.SecurePassword -syncHash $syncHash -clearAfterSeconds 5 -random $Global:togglePwRnd.IsChecked
})
$Global:togglePwRnd = $syncHash.Window.FindName("togglePwRnd")
$Global:togglePwInd = $syncHash.Window.FindName("togglePwInd")
$togglePwInd.IsEnabled = $false
$togglePwRnd.Add_Click(
{
# Disable new password boxes
$pwdBoxNew1.IsEnabled = !$togglePwRnd.IsChecked
$pwdBoxNew2.IsEnabled = !$togglePwRnd.IsChecked
# Reset new password box content
$pwdBoxNew1.Password = ""
$pwdBoxNew2.Password = ""
# Enable set password button and option to set password for each account random
$setNewPswdBtn.IsEnabled = $togglePwRnd.IsChecked
$togglePwInd.IsEnabled = $togglePwRnd.IsChecked
})
#######################################
######## Fly out
##########################
# toggle switch
$Global:toggleIsTop = $syncHash.Window.FindName("windowStayTop")
#######################################
######## Add_Click
##########################
# Create click event for dynamic button -> set current password
[System.Windows.RoutedEventHandler]$funcTestCredentials = {
param ($sender)
Set-PasswordOverlay -dataContext $sender.DataContext
}
# Create click event for dynamic button -> set new password
[System.Windows.RoutedEventHandler]$funcSetCredentials = {
param ($sender)
Set-NewPasswordOverlay -dataContext $sender.DataContext
}
# Create click event for dynamic button -> copy password to clip board
[System.Windows.RoutedEventHandler]$funcCopyToClipBoard = {
param ($sender)
Set-RunSpaceContentToClipBoard -password $sender.DataContext.pswdNew -syncHash $syncHash -clearAfterSeconds 5
}
$Global:btnFlyOut.Add_Click(
{
$syncHash.flyOut.IsOpen = (!($syncHash.flyOut.IsOpen))
})
$Global:toggleIsTop.Add_Click( { Set-WindowStayTop })
#######################################
######## XML datatemplate and bindings
##########################
###### Verify password data template
# Find gridviewcolumn we will attach our datatemplate to
$verifyBtnDataTemp = $syncHash.Window.FindName("verifyBtnDataTemp")
# Create bindings
$bindPwdCurText = New-Object System.Windows.Data.Binding
$bindPwdCurText.path = "pswdVerifyBtnText"
$bindPwdVisibility = New-Object System.Windows.Data.Binding
$bindPwdVisibility.path = "pswdVerifyBtnVisible"
$bindPwdEnabled = New-Object System.Windows.Data.Binding
$bindPwdEnabled.path = "pswdVerifyBtnEnabled"
# Create button factory
$buttonFactory = New-Object System.Windows.FrameworkElementFactory([System.Windows.Controls.Button])
$buttonFactory.AddHandler([System.Windows.Controls.Button]::ClickEvent, [System.Windows.RoutedEventHandler]$funcTestCredentials)
# Add bindings
$buttonFactory.SetBinding([System.Windows.Controls.Button]::ContentProperty, $bindPwdCurText)
$buttonFactory.SetBinding([System.Windows.Controls.Button]::VisibilityProperty, $bindPwdVisibility)
$buttonFactory.SetBinding([System.Windows.Controls.Button]::IsEnabledProperty, $bindPwdEnabled)
$verifyBtnDataTemp.CellTemplate.VisualTree = $buttonFactory
###### Set password data template
$setBtnDataTemp = $syncHash.Window.FindName("setBtnDataTemp")
$pswdSetBtnText = New-Object System.Windows.Data.Binding
$pswdSetBtnVisible = New-Object System.Windows.Data.Binding
$pswdSetBtnEnabled = New-Object System.Windows.Data.Binding
$pswdSetBtnText.path = "pswdSetBtnText"
$pswdSetBtnVisible.path = "pswdSetBtnVisible"
$pswdSetBtnEnabled.path = "pswdSetBtnEnabled"
$buttonFactory = New-Object System.Windows.FrameworkElementFactory([System.Windows.Controls.Button])
$buttonFactory.AddHandler([System.Windows.Controls.Button]::ClickEvent, [System.Windows.RoutedEventHandler]$funcSetCredentials)
# Add bindings
$buttonFactory.SetBinding([System.Windows.Controls.Button]::ContentProperty, $pswdSetBtnText)
$buttonFactory.SetBinding([System.Windows.Controls.Button]::VisibilityProperty, $pswdSetBtnVisible)
$buttonFactory.SetBinding([System.Windows.Controls.Button]::IsEnabledProperty, $pswdSetBtnEnabled)
$setBtnDataTemp.CellTemplate.VisualTree = $buttonFactory
###### Clip board data template
$dataTempClipBoard = $syncHash.Window.FindName("dataTempClipBoard")
$clipBoardBtnVisible = New-Object System.Windows.Data.Binding
$clipBoardBtnVisible.path = "clipBoardBtnVisible"
$buttonFactory = New-Object System.Windows.FrameworkElementFactory([System.Windows.Controls.Button])
$buttonFactory.AddHandler([System.Windows.Controls.Button]::ClickEvent, [System.Windows.RoutedEventHandler]$funcCopyToClipBoard)
$buttonFactory.SetValue([System.Windows.Controls.Button]::ContentProperty, "Copy to Clipboard")
# Add bindings
$buttonFactory.SetBinding([System.Windows.Controls.Button]::VisibilityProperty, $clipBoardBtnVisible)
$dataTempClipBoard.CellTemplate.VisualTree = $buttonFactory
#######################################
######## Run main functions
##########################
Handle-Configuration -Option load
Reset-App
$async = $syncHash.Window.Dispatcher.InvokeAsync( {
$syncHash.Window.ShowDialog() | Out-Null
})
$async.Wait() | Out-Null
Handle-Configuration -Option save