-
Notifications
You must be signed in to change notification settings - Fork 15
/
Show-InputForm.ps1
725 lines (652 loc) · 36.5 KB
/
Show-InputForm.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
<#
.SYNOPSIS
PowerShell GUI form for data input
.DESCRIPTION
Multiple use GUI for asking for a variety of input types that can also perform simple validation.
Input type includes:
Simple text box
List of check boxes
List of text boxes
Dropdown selection
Large multiline text box
Validation can be enabled for checking input is in one of the following formats:
Alphabetic (Letters A-Z only)
Numerical
Integer
Decimal
URL (only http(s):// , (s)ftp(s)://)
Email Address
IPv4
IPv6
The 'list of text boxes' input type will also validate for duplicated entries
.PARAMETER Type
The type of input form to show
One of the following: 'Simple', 'Password', 'Check', 'Option', 'List', 'Multi' or 'Large'
.PARAMETER Title
The window title for the input form
.PARAMETER Description
Long description text of what the user is expected to enter into the form
.PARAMETER Validation
One of the following: 'None', 'AZ', 'Numeric', 'Integer', 'Decimal', 'Symbol', 'File', 'URL', 'Email', 'IPv4' or 'IPv6'
Can only be used with the following types: 'Simple', 'Password' and 'List'
.PARAMETER InputList
One or more entries for the 'Check', 'Option' or 'List' input types
.PARAMETER CurrentValue
One or more entries that are pre-selected for the 'Check' type, already filled in for the 'List' type.
Use a single entry for the pre-selected 'Option' type
.PARAMETER EnableKeePass
Allows the use of asking for a KeePass password database entry and field name
Can only be used with the following types: 'Simple', 'Password', 'Multi' and 'Large'
.INPUTS
None
.OUTPUTS
System.String or System.String[]
Return value depends on input form Type
Will return '!!-CANCELLED-!!' if the form is cancelled
.EXAMPLE
Show-InputForm -Type 'Simple' -Title 'Input Form' -Description 'Enter your name'
Simple input box with no validation.
.EXAMPLE
Show-InputForm -Type 'Simple' -Title 'Numerical Input' -Description 'Enter correct threshold value' -Validation 'Integer' -CurrentValue '32'
Simple input box, prepopulated with a value of 32. Accepts only integer numbers.
.EXAMPLE
Show-InputForm -Type 'Check' -Title 'CheckBox List' -Description 'Select one or more options from the list below' -InputList ('Black','Blue','Cyan','Green','Red','Yellow','White') -CurrentValue ('Blue','Green')
A list of multi-selectable check boxes, with 'Blue' and 'Green' automatically ticked.
.EXAMPLE
Show-InputForm -Type 'Option' -Title 'Option List' -Description 'Select your favourite colour' -InputList ('Black','Blue','Cyan','Green','Red','Yellow','White')
A single drop down list populated with colours, none are pre-selected.
.EXAMPLE
Show-InputForm -Type 'List' -Title 'Multiple Items' -Description 'Enter all the addresses you want to send an email to' -Validation 'Email' -CurrentValue ('[email protected]','[email protected]')
A list of simple text boxes allowing for multiple text entries, all with email address validation. Two addresses have already been added.
.EXAMPLE
Show-InputForm -Type 'Large' -Title 'Larger Text' -Description 'Enter your public certificate below'
Simple multi-line input box that does not keep line breaks.
Use -Type 'Multi' for line break support
.LINK
http://myrandomthoughts.co.uk
.LINK
#>
Function Show-InputForm
{
Param
(
[parameter(Mandatory=$true )] [ValidateSet('Simple', 'Password', 'Check', 'Option', 'List', 'Multi', 'Large')]
[string] $Type,
[parameter(Mandatory=$true )] [string] $Title,
[parameter(Mandatory=$true )] [string] $Description,
[parameter(Mandatory=$false)] [ValidateSet('None', 'AZ', 'Numeric', 'Integer', 'Decimal', 'Symbol', 'File', 'URL', 'Email', 'IPv4', 'IPv6')]
[string] $Validation = 'None',
[parameter(Mandatory=$false)] [string[]]$InputList,
[parameter(Mandatory=$false)] [string[]]$CurrentValue,
[parameter(Mandatory=$false)] [boolean] $EnableKeePass = $false
)
[Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null
[Reflection.Assembly]::LoadWithPartialName('System.Data') | Out-Null
[Reflection.Assembly]::LoadWithPartialName('System.Drawing') | Out-Null
[System.Drawing.Font]$sysFont = [System.Drawing.SystemFonts]::MessageBoxFont
[System.Windows.Forms.Application]::EnableVisualStyles()
#region Form Scripts
$ChkButton_Click = {
If ($ChkButton.Text -eq 'Check All') { $ChkButton.Text = 'Check None'; [boolean]$checked = $true } Else { $ChkButton.Text = 'Check All'; [boolean]$checked = $False }
ForEach ($Control In $frm_Main.Controls) { If ($control -is [System.Windows.Forms.CheckBox]) { $control.Checked = $checked } }
}
$AddButton_Click = { AddButton_Click -Value '' -Override $false -Type 'TEXT' }
Function AddButton_Click ( [string]$Value, [boolean]$Override, [string]$Type )
{
[int]$BoxNumber = 0
ForEach ($Control In $frm_Main.Controls) { If (($Control -is [System.Windows.Forms.TextBox]) -or ($Control -is [System.Windows.Forms.CheckBox])) { $BoxNumber++ } }
If ($Type -eq 'TEXT')
{
ForEach ($control In $frm_Main.Controls) {
If ($control -is [System.Windows.Forms.TextBox]) {
[System.Windows.Forms.TextBox]$isEmtpy = $null
If ([string]::IsNullOrEmpty($control.Text) -eq $True) { $isEmtpy = $control; Break }
}
}
If ($Override -eq $true) { $isEmtpy = $null }
If ($isEmtpy -ne $null)
{
$isEmtpy.Select()
$isEmtpy.Text = $Value
Return
}
}
# Increase form size, move buttons down, add new field
$numberOfTextBoxes++
$frm_Main.ClientSize = "394, $(147 + ($BoxNumber * 26))"
$btn_Accept.Location = "307, $(110 + ($BoxNumber * 26))"
$btn_Cancel.Location = "220, $(110 + ($BoxNumber * 26))"
If ($Type -eq 'TEXT')
{
$AddButton.Location = " 39, $(110 + ($BoxNumber * 26))"
# Add new counter label
$labelCounter = New-Object 'System.Windows.Forms.Label'
$labelCounter.Location = " 12, $(75 + ($BoxNumber * 26))"
$labelCounter.Size = ' 21, 20'
$labelCounter.Font = $sysFont
$labelCounter.Text = "$($BoxNumber + 1):"
$labelCounter.TextAlign = 'MiddleRight'
$frm_Main.Controls.Add($labelCounter)
# Add new text box and select it for focus
$textBox = New-Object 'System.Windows.Forms.TextBox'
$textBox.Location = " 39, $(75 + ($BoxNumber * 26))"
$textBox.Size = '343, 20'
$textBox.Font = $sysFont
$textBox.Name = "textBox$BoxNumber"
$textBox.Text = $Value.Trim()
$frm_Main.Controls.Add($textBox)
$frm_Main.Controls["textbox$BoxNumber"].Select()
}
ElseIf ($Type -eq 'CHECK')
{
# Add new check box
$chkBox = New-Object 'System.Windows.Forms.CheckBox'
$chkBox.Location = " 12, $(75 + ($BoxNumber * 26))"
$chkBox.Size = '370, 20'
$chkBox.Font = $sysFont
$chkBox.Name = "chkBox$BoxNumber"
$chkBox.Text = $Value
$chkBox.TextAlign = 'MiddleLeft'
$frm_Main.Controls.Add($chkBox)
$frm_Main.Controls["chkbox$BoxNumber"].Select()
}
Else { }
}
$kp_comboBox_SelectedIndexChanged = {
Try {
If ($kp_comboBox.SelectedIndex -eq ($kp_comboBox.Items.Count - 1)) { $kp_CustomField.Enabled = $True; $kp_CustomField.Text = '' }
Else { $kp_CustomField.Enabled = $False; $kp_CustomField.Text = $kp_comboBox.SelectedItem }
} Catch {}
}
$KeePassButton_Click = { Change-Form -ChangeTo ($KeePassButton.Text) }
Function Change-Form ( [string]$ChangeTo )
{
If ($ChangeTo -eq 'KeePass') # Change to KeePass mode
{
# Hide Fields
$textBox.Visible = $False
$pic_InvalidValue.Visible = $False
# Show Fields
$kp_textBox.Visible = $True
$kp_EntryLabel.Visible = $True
$kp_FieldLabel.Visible = $True
$kp_comboBox.Visible = $True
$kp_CustomField.Visible = $True
$kp_comboBox_SelectedIndexChanged.Invoke()
$kp_textBox.Select()
# Resize form
$frm_Main.ClientSize = "394, $(147 + 62)"
$btn_Accept.Location = "307, $(110 + 62)" # 62 comes from
$btn_Cancel.Location = "220, $(110 + 62)" # VB.net IDE
$KeePassButton.Location = " 12, $(110 + 62)" #
$KeePassButton.Text = 'Simple'
$KeePassButton.TabIndex = '199'
}
ElseIf (($Type -eq 'Multi') -or ($Type -eq 'Large'))
{
# Hide Fields
$kp_textBox.Visible = $False
$kp_EntryLabel.Visible = $False
$kp_FieldLabel.Visible = $False
$kp_comboBox.Visible = $False
$kp_CustomField.Visible = $False
$pic_InvalidValue.Visible = $False
# Show Fields
$textBox.Visible = $True
$textBox.Select()
# Resize form
$frm_Main.ClientSize = "394, $(147 + 104)"
$btn_Accept.Location = "307, $(110 + 104)"
$btn_Cancel.Location = "220, $(110 + 104)"
$KeePassButton.Location = " 12, $(110 + 104)"
$KeePassButton.Text = 'KeePass'
$KeePassButton.TabIndex = '199'
}
Else
{
# Hide Fields
$kp_textBox.Visible = $False
$kp_EntryLabel.Visible = $False
$kp_FieldLabel.Visible = $False
$kp_comboBox.Visible = $False
$kp_CustomField.Visible = $False
$pic_InvalidValue.Visible = $False
# Show Fields
$textBox.Visible = $True
$textBox.Select()
# Resize form
$frm_Main.ClientSize = '394, 147'
$btn_Accept.Location = '307, 110'
$btn_Cancel.Location = '220, 110'
$KeePassButton.Location = ' 12, 110'
$KeePassButton.Text = 'KeePass'
$KeePassButton.TabIndex = '199'
}
}
# Start form validation and make sure everything entered is correct
$btn_Accept_Click = {
[string[]]$currentValues = @('')
[boolean] $ValidatedInput = $true
ForEach ($Control In $frm_Main.Controls)
{
If (($Control -is [System.Windows.Forms.TextBox]) -and ($Control.Visible -eq $true))
{
$Control.BackColor = 'Window'
If (($Type -eq 'LIST') -and ($Control.Text.Contains(';') -eq $true))
{
[string[]]$ControlText = ($Control.Text).Split(';')
$Control.Text = '' # Remove current data so that it can be used as a landing control for the split data
ForEach ($item In $ControlText) { AddButton_Click -Value $item -Override $false -Type 'TEXT' }
}
}
}
# Reset Control Loop for any new fields that may have been added
ForEach ($Control In $frm_Main.Controls)
{
If (($Control -is [System.Windows.Forms.TextBox]) -and ($Control.Visible -eq $true))
{
$ValidatedInput = $(ValidateInputBox -Control $Control)
$pic_InvalidValue.Image = $img_Input.Images[0]
$pic_InvalidValue.Tag = 'Validation failed for current value'
$ToolTip.SetToolTip($pic_InvalidValue, $pic_InvalidValue.Tag)
If ($ValidatedInput -eq $true)
{
Try { If ($kp_comboBox.Visible -eq $true) { $currentValues = '' } } Catch { } # Skip duplication check if using KeePass
If (($Type -eq 'LIST') -and (([string]::IsNullOrEmpty($Control.Text) -eq $false) -and ($currentValues -contains ($Control.text))))
{
$ValidatedInput = $false
$pic_InvalidValue.Image = $img_Input.Images[1]
$pic_InvalidValue.Tag = 'Duplicated value found'
$ToolTip.SetToolTip($pic_InvalidValue, $pic_InvalidValue.Tag)
$Control.BackColor = 'Info'
}
Else { $currentValues += $Control.Text }
}
If ($ValidatedInput -eq $false)
{
$pic_InvalidValue.Location = "331, $([math]::Round(($Control.Height -16) / 2) + $Control.Top)" # 331 = $($($Control.Left) + $($Control.Width) - (48 + 3))
$pic_InvalidValue.Visible = $true
$Control.Focus()
$Control.SelectAll()
$ToolTip.Show($pic_InvalidValue.Tag, $pic_InvalidValue, 36, 12, 2500)
$Control.BackColor = 'Info'
Break
}
}
}
$currentValues = $null
If ($ValidatedInput -eq $true) { $frm_Main.DialogResult = [System.Windows.Forms.DialogResult]::OK }
}
Function ValidateInputBox ([System.Windows.Forms.Control]$Control)
{
$Control.Text = ($Control.Text.Trim())
[boolean]$ValidateResult = $false
[string] $StringToCheck = $($Control.Text)
# Ignore ValidateAgainst if KeePass is used
Try { If ($kp_comboBox.Visible -eq $true) { Return (-not [string]::IsNullOrEmpty($StringToCheck)) } } Catch { }
# Ignore for MULTI and LARGE fields
If (($Type -eq 'LARGE') -or ($Type -eq 'MULTI')) { Return $true }
# Ignore control if empty
If ([string]::IsNullOrEmpty($StringToCheck) -eq $true) { Return $true }
# Validate
Switch ($Validation)
{
'AZ' { $ValidateResult = ($StringToCheck -match "^[A-Za-z]+$"); Break } # Letters only (A-Za-z)
'Numeric' { $ValidateResult = ($StringToCheck -match '^(-)?([\d]+)?\.?[\d]+$'); Break } # Both integer and decimal numbers
'Integer' { $ValidateResult = ($StringToCheck -match '^(-)?[\d]+$'); Break } # Integer numbers only
'Decimal' { $ValidateResult = ($StringToCheck -match '^(-)?[\d]+\.[\d]+$'); Break } # Decimal numbers only
'Symbol' { $ValidateResult = ($StringToCheck -match '^[^A-Za-z0-9]+$'); Break } # Any symbol (not numbers or letters)
'File' { # Valid file or folder name
$StringToCheck = $StringToCheck.TrimEnd('\')
$ValidateResult = ($StringToCheck -match "^(?:[a-zA-Z]\:|\\\\[\w\.]+\\[\w.$]+)\\(?:[\w]+\\)*\w([\w.])+$")
Break
}
'URL' { # URL
[url] $url = ''
[boolean]$ValidURL1 = ($StringToCheck -match '^(ht|(s)?f|)tp(s)?:\/\/(.*)\/([a-z]+\.[a-z]+)') # http(s):// or (s)ftp(s)://
[boolean]$ValidURL2 = ([System.Uri]::TryCreate($StringToCheck, [System.UriKind]::Absolute, [ref]$url))
$ValidateResult = ($ValidURL1 -and $ValidURL2)
Break
}
'Email' { # [email protected]
Try { $ValidateResult = (($StringToCheck -as [System.Net.Mail.MailAddress]).Address -eq $StringToCheck) }
Catch { $ValidateResult = $false }
Break
}
'IPv4' { # IPv4 address (1.2.3.4)
[boolean]$Octets = (($StringToCheck.Split('.') | Measure-Object).Count -eq 4)
[boolean]$ValidIP = ($StringToCheck -as [ipaddress]) -as [boolean]
$ValidateResult = ($ValidIP -and $Octets)
Break
}
'IPv6' { # IPv6 address (REGEX from 'https://www.powershellgallery.com/packages/IPv6Regex/1.1.1')
[string]$IPv6 = @"
^((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?[0-9])|:))|(([0-9a-f]
{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?[0-9])|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]
{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?[0-9]))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|
2[0-4]\d|1\d\d|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?[0-9]))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]
?[0-9])\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?[0-9]))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?[0-9])\.){3}(25[0-5]|
2[0-4]\d|1\d\d|[1-9]?[0-9]))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?[0-9]))|:)))$
"@
$ValidateResult = ($StringToCheck -match $IPv6)
Break
}
Default { # No Validation
$ValidateResult = $true
}
}
Return $ValidateResult
}
$frm_Main_Cleanup_FormClosed = {
Try {
$btn_Accept.Remove_Click($btn_Accept_Click)
$AddButton.Remove_Click($AddButton_Click)
$KeePassButton.Remove_Click($KeePassButton_Click)
} Catch {}
$frm_Main.Remove_FormClosed($frm_Main_Cleanup_FormClosed)
}
#endregion
#region Input Form Controls
[System.Windows.Forms.Application]::EnableVisualStyles()
$frm_Main = New-Object 'System.Windows.Forms.Form'
$frm_Main.FormBorderStyle = 'FixedDialog'
$frm_Main.MaximizeBox = $False
$frm_Main.MinimizeBox = $False
$frm_Main.ControlBox = $False
$frm_Main.Text = " $Title"
$frm_Main.ShowInTaskbar = $True
$frm_Main.AutoScaleDimensions = '6, 13'
$frm_Main.AutoScaleMode = 'Font'
$frm_Main.ClientSize = '394, 147' # 400 x 175
$frm_Main.StartPosition = 'CenterScreen'
$ToolTip = New-Object 'System.Windows.Forms.ToolTip'
# 48x16 Image List for INVALID and DUPLICATE error message icons
$img_Input = New-Object 'System.Windows.Forms.ImageList'
$img_Input.TransparentColor = 'Transparent'
$img_Input_binaryFomatter = New-Object 'System.Runtime.Serialization.Formatters.Binary.BinaryFormatter'
$img_Input_MemoryStream = New-Object 'System.IO.MemoryStream' (,[byte[]][System.Convert]::FromBase64String('
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkD
AAAADwMAAADkCgAAAk1TRnQBSQFMAgEBAgEAAQgBAAEIAQABMAEAARABAAT/ASEBAAj/AUIBTQE2BwABNgMAASgDAAHAAwABEAMAAQEBAAEgBgABMP8A/wD/AP8AGgADNAFxAzQBiP8A/wD6AAMyAZkDKwG2/wD/APoAAzIBmQMrAbb/AP8ANgADNAGPAzUBeQQAAy0BUAMvAaoEAAM1AXkDNQGBCAADEQEX
AywBswwAAyMBNgMqAboDMwFqAygBvwMcASgDDgESAygBvwMcASgDDgESAygBvwMcASgEAAMxAV0DKgG6AzMBZwMoAb8YAAMtAbEDKAG/Ays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C/wEkAYABAAEBAfkBBAGSAUEBkgFBDAAC/wEkAYIBAAEBAfkBBAGSAUkBggFBDAAC/wEgAZIBAAEhAfkBJAGCAUgBAAEhDAAC/wEhAZMBAAEhAfkBJAGGAUwBwAEzDAAC/wE/Af8B
8QF5AfgBPwH+AU8B/gF/DAAC/wE/Af8B8AE5AfgBPwH+AU8C/wwAAv8BPwH/AfEBeQH4Af8B/gFPAv8MAAz/DAAL'))
$img_Input.ImageStream = $img_Input_binaryFomatter.Deserialize($img_Input_MemoryStream)
$img_Input_binaryFomatter = $null
$img_Input_MemoryStream = $null
$pic_InvalidValue = New-Object 'System.Windows.Forms.PictureBox'
$pic_InvalidValue.BackColor = 'Info'
$pic_InvalidValue.Location = ' 0, 0'
$pic_InvalidValue.Size = ' 48, 16'
$pic_InvalidValue.Visible = $false
$pic_InvalidValue.TabStop = $False
$pic_InvalidValue.BringToFront()
$frm_Main.Controls.Add($pic_InvalidValue)
$lbl_Description = New-Object 'System.Windows.Forms.Label'
$lbl_Description.Location = ' 12, 12'
$lbl_Description.Size = '370, 48'
$lbl_Description.Font = $sysFont
$lbl_Description.Text = $($Description.Trim())
$frm_Main.Controls.Add($lbl_Description)
If ($Validation -ne 'None')
{
$lbl_Validation = New-Object 'System.Windows.Forms.Label'
$lbl_Validation.Location = '212, 60'
$lbl_Validation.Size = '170, 15'
$lbl_Validation.Font = $sysFont
$lbl_Validation.Text = "Validation: $($Validation.ToUpper())"
$lbl_Validation.TextAlign = 'BottomRight'
$frm_Main.Controls.Add($lbl_Validation)
}
$btn_Accept = New-Object 'System.Windows.Forms.Button'
$btn_Accept.Location = '307, 110'
$btn_Accept.Size = ' 75, 25'
$btn_Accept.Font = $sysFont
$btn_Accept.Text = 'OK'
$btn_Accept.TabIndex = '97'
$btn_Accept.Add_Click($btn_Accept_Click)
If (($Type -ne 'MULTI') -and ($Type -ne 'LARGE')) { $frm_Main.AcceptButton = $btn_Accept }
$frm_Main.Controls.Add($btn_Accept)
$btn_Cancel = New-Object 'System.Windows.Forms.Button'
$btn_Cancel.Location = '220, 110'
$btn_Cancel.Size = ' 75, 25'
$btn_Cancel.Font = $sysFont
$btn_Cancel.Text = 'Cancel'
$btn_Cancel.TabIndex = '98'
$btn_Cancel.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$frm_Main.CancelButton = $btn_Cancel
$frm_Main.Controls.Add($btn_Cancel)
$frm_Main.Add_FormClosed($frm_Main_Cleanup_FormClosed)
#endregion
#region Input Form Controls Part 2
Switch ($Type)
{
'LIST' {
# List of text boxes
[int]$itemCount = ($CurrentValue.Count)
If ($itemCount -ge 5) { [int]$numberOfTextBoxes = $itemCount + 1 } Else { [int]$numberOfTextBoxes = 5 }
If (-not $CurrentValue) { $CurrentValue = "" }
$numberOfTextBoxes-- # Count from zero
# Add 'Add' button
$AddButton = New-Object 'System.Windows.Forms.Button'
$AddButton.Location = " 39, $(110 + ($numberOfTextBoxes * 26))"
$AddButton.Size = ' 75, 25'
$AddButton.Font = $sysFont
$AddButton.Text = 'Add'
$AddButton.Add_Click($AddButton_Click)
$frm_Main.Controls.Add($AddButton)
# Add initial textboxes
For ($i = 0; $i -le $numberOfTextBoxes; $i++) { AddButton_Click -Value ($CurrentValue[$i]) -Override $true -Type 'TEXT' }
$frm_Main.Controls['textbox0'].Select()
Break
}
'CHECK' {
# Add 'Check All' button
$ChkButton = New-Object 'System.Windows.Forms.Button'
$ChkButton.Location = " 12, $(110 + (($InputList.Count -1) * 26))"
$ChkButton.Size = '125, 25'
$ChkButton.Font = $sysFont
$ChkButton.Text = 'Check All'
$ChkButton.Add_Click($ChkButton_Click)
$frm_Main.Controls.Add($ChkButton)
# Add initial textboxes
[int]$i = 0
ForEach ($item In $InputList)
{
AddButton_Click -Value ($item.Trim()) -Override $true -Type 'CHECK'
If ([string]::IsNullOrEmpty($CurrentValue) -eq $false) { If ($CurrentValue.Contains($item.Trim())) { $frm_Main.Controls["chkBox$i"].Checked = $true } }
$i++
}
Break
}
'OPTION' {
# Drop down selection list
$comboBox = New-Object 'System.Windows.Forms.ComboBox'
$comboBox.Location = ' 12, 75'
$comboBox.Size = '370, 21'
$comboBox.Font = $sysFont
$comboBox.DropDownStyle = 'DropDownList'
$frm_Main.Controls.Add($comboBox)
[string[]]$InputTrim = @()
$InputList | ForEach { $InputTrim += ($_.Trim()) }
$comboBox.Items.AddRange($InputTrim) | Out-Null
$frm_Main.Add_Shown({$comboBox.Select()})
If ([string]::IsNullOrEmpty($CurrentValue) -eq $false) { $comboBox.SelectedItem = $CurrentValue[0] } Else { $comboBox.SelectedIndex = -1 }
Break
}
{($_ -eq 'MULTI') -or ($_ -eq 'LARGE')} {
# Multi-line text entry
$textBox = New-Object 'System.Windows.Forms.TextBox'
$textBox.Location = ' 12, 75'
$textBox.Size = '370, 124'
$textBox.Font = $sysFont
$textBox.Multiline = $True
$textBox.ScrollBars = 'Vertical'
$frm_Main.Controls.Add($textBox)
$frm_Main.Add_Shown({$textBox.Select()})
$textBox.Select()
# Add KeePass entry button
$KeePassButton = New-Object 'System.Windows.Forms.Button'
$KeePassButton.Location = " 12, $(110 + 104)" #
$KeePassButton.Size = ' 75, 25'
$KeePassButton.Font = $sysFont
$KeePassButton.Text = 'KeePass'
$KeePassButton.TabIndex = '99'
$KeePassButton.Visible = $EnableKeePass
$KeePassButton.Add_Click($KeePassButton_Click)
$frm_Main.Controls.Add($KeePassButton)
# Resize form
$frm_Main.Height += 104 #
$btn_Accept.Location = "307, $(110 + 104)" # 104 comes from 4 x 26
$btn_Cancel.Location = "220, $(110 + 104)" #
Break
}
{($_ -eq 'SIMPLE') -or ($_ -eq 'PASSWORD')} {
# Add default text box
$textBox = New-Object 'System.Windows.Forms.TextBox'
$textBox.Location = ' 12, 75'
$textBox.Size = '370, 20'
$textBox.Font = $sysFont
If ($Type -eq 'PASSWORD') { $textBox.PasswordChar = 'o' }
$frm_Main.Controls.Add($textBox)
$textBox.Select()
# Add KeePass entry button
$KeePassButton = New-Object 'System.Windows.Forms.Button'
$KeePassButton.Location = ' 12, 110'
$KeePassButton.Size = ' 75, 25'
$KeePassButton.Font = $sysFont
$KeePassButton.Text = 'KeePass'
$KeePassButton.TabIndex = '199'
$KeePassButton.Visible = $EnableKeePass
$KeePassButton.Add_Click($KeePassButton_Click)
$frm_Main.Controls.Add($KeePassButton)
Break
}
Default { Write-Warning "Invalid Input Form Type: $Type" }
}
If (('SIMPLE', 'PASSWORD', 'MULTI', 'LARGE') -contains $Type)
{
# ### KEEPASS FIELDS ###
# Add KeePass Field
$kp_textBox = New-Object 'System.Windows.Forms.TextBox'
$kp_textBox.Location = ' 69, 75'
$kp_textBox.Size = '313, 20'
$kp_textBox.Font = $sysFont
$kp_textBox.Visible = $False
$frm_Main.Controls.Add($kp_textBox)
$kp_textBox.Select()
# Add KeePass label
$kp_EntryLabel = New-Object 'System.Windows.Forms.Label'
$kp_EntryLabel.Location = ' 12, 75'
$kp_EntryLabel.Size = ' 51, 20'
$kp_EntryLabel.Font = $sysFont
$kp_EntryLabel.Text = 'Entry :'
$kp_EntryLabel.TextAlign = 'MiddleRight'
$kp_EntryLabel.Visible = $False
$frm_Main.Controls.Add($kp_EntryLabel)
# All KeePass label
$kp_FieldLabel = New-Object 'System.Windows.Forms.Label'
$kp_FieldLabel.Location = ' 12, 110'
$kp_FieldLabel.Size = ' 51, 20'
$kp_FieldLabel.Font = $sysFont
$kp_FieldLabel.Text = 'Field :'
$kp_FieldLabel.TextAlign = 'MiddleRight'
$kp_FieldLabel.Visible = $False
$frm_Main.Controls.Add($kp_FieldLabel)
# Add KeePass dropdown list
$kp_comboBox = New-Object 'System.Windows.Forms.ComboBox'
$kp_comboBox.Location = ' 69, 110'
$kp_comboBox.Size = '313, 21'
$kp_comboBox.Font = $sysFont
$kp_comboBox.DropDownStyle = 'DropDownList'
$kp_comboBox.Visible = $False
$kp_comboBox.Add_SelectedIndexChanged($kp_comboBox_SelectedIndexChanged)
$kp_comboBox.Items.Clear()
$kp_comboBox.Items.AddRange(@('UserName', 'Password', 'URL', 'Notes', '*Custom Field*'))
$frm_Main.Controls.Add($kp_comboBox)
$kp_comboBox.SelectedItem = 'UserName'
# Add KeePass text field
$kp_CustomField = New-Object 'System.Windows.Forms.TextBox'
$kp_CustomField.Location = ' 69, 137'
$kp_CustomField.Size = '313, 20'
$kp_CustomField.Font = $sysFont
$kp_CustomField.Enabled = $False
$kp_CustomField.Visible = $False
$frm_Main.Controls.Add($kp_CustomField)
If ([string]::IsNullOrEmpty($CurrentValue) -eq $false)
{
If (($CurrentValue[0].StartsWith('UseKeePass|') -eq $True) -and ($EnableKeePass -eq $true))
{
Change-Form -ChangeTo 'KeePass' | Out-Null
$kp_textBox.Text = (($CurrentValue[0].Split('|')[1]).Trim())
$kp_comboBox.SelectedItem = '*Custom Field*' # Select 'Custom' as the default
$kp_comboBox.SelectedItem = (($CurrentValue[0].Split('|')[2]).Trim())
$kp_CustomField.Text = (($CurrentValue[0].Split('|')[2]).Trim())
}
Else
{
Change-Form -ChangeTo 'Simple' | Out-Null
$textBox.Text = (($CurrentValue.Trim()) -join "`r`n")
}
}
Else
{
Change-Form -ChangeTo 'Simple' | Out-Null
}
}
#endregion
#region Show Form And Return Value
ForEach ($control In $frm_Main.Controls) { $control.Font = $sysFont; Try { $control.FlatStyle = 'Standard' } Catch {} }
$result = $frm_Main.ShowDialog($MainForm)
If ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
If (([string]::IsNullOrEmpty($(Get-Variable -Name KeePassButton -ErrorAction SilentlyContinue)) -eq $False) -and ($KeePassButton.Text -eq 'Simple'))
{
Return "UseKeePass|$($kp_textBox.Text)|$($kp_CustomField.Text)"
}
Else
{
Switch ($Type)
{
'LIST' {
[string[]]$return = @()
ForEach ($control In $frm_Main.Controls) { If ($control -is [System.Windows.Forms.TextBox]) {
If ([string]::IsNullOrEmpty($control.Text) -eq $false) { $return += ($($control.Text.Trim())) } }
} Return $return
}
'CHECK' {
[string[]]$return = @()
ForEach ($Control In $frm_Main.Controls) { If ($control -is [System.Windows.Forms.CheckBox]) {
If ($control.Checked -eq $true) { $return += ($($control.Text.Trim())) } }
} Return $return
}
'LARGE' {
Do { [string]$return = $($textBox.Text.Trim()).Replace("`r`n", ' ') }
While ( $return.IndexOf("`r`n") -gt -1 ); Return ($return.Trim("`r`n"))
}
{($_ -eq 'SIMPLE') -or ($_ -eq 'PASSWORD')} {
Do { [string]$return = $($textBox.Text.Trim()).Replace("`r`n", ' ') }
While ( $return.IndexOf("`r`n") -gt -1 ); Return ($return.Trim("`r`n"))
}
'MULTI' { Return $($textBox.Text.Trim()) }
'OPTION' { Return $($comboBox.SelectedItem) }
Default { Return "Invalid return type: $Type" }
}
}
}
ElseIf ($result -eq [System.Windows.Forms.DialogResult]::Cancel) { Return '!!-CANCELLED-!!' }
#endregion
}