-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOX-Module.psm1
16076 lines (11998 loc) · 668 KB
/
BOX-Module.psm1
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS
Powershell module that contains all function used.
.DESCRIPTION
Powershell module that contains all function used.
This module is lincked to script file : .\BBOX-Administration.ps1
Be carefull some variables are linked from main script Box-Administration.ps1 to :
- $global:JSONSettingsProgramContent
- $global:JSONSettingsCurrentUserContent
- $global:JSONSettingsDefaultUserContent
- ...
Also be carefull some Global variables are not as a parameter from functions but directly consume in functions
Examples :
- $global:BoxType
- $global:TriggerExitSystem
- $global:CredentialsTarget
- $global:LogDateFolderNamePath
- $global:LogFileName
- $Global:journal
- $global:TriggerDialogBox
.EXAMPLE
To import this module :
Import-Module '.\BOX-Module.psm1'
To get all information from this module
Get-module -Name 'BOX-Module'
.INPUTS
Functions with parameters or not
.OUTPUTS
Data that transformed by functions
Windows Forms interraction
.NOTES
Version : 2.7
Creation Date : 2020/04/30
Updated Date : 2024/01/29
Updated By : @Zardrilokis => [email protected]
Author : @Zardrilokis => [email protected]
#>
#region GLOBAL Functions
#region Add AssemblyName Classies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#endregion Add AssemblyName Classies
#region Logs Files
function Write-Log {
<#
.SYNOPSIS
Write-Log allow to write fonctionnal execution logs
.DESCRIPTION
Write a log in the host console and to a csv file.
.PARAMETER Type
Indicate which type of log
Valid values : 'INFO','INFONO','VALUE','WARNING','ERROR','DEBUG'
Defaut value : 'INFO'
.PARAMETER Message
Write the message you want to display
.PARAMETER Name
Use to structure the log by categories
.PARAMETER NotDisplay
Use if you don't want to display the 'message' parameter to the Host Console
.PARAMETER Logname
This is the full path of the log file
.EXAMPLE
Message will be displayed in the PowerShell Host Console and in the log file :
Write-Log -Type INFO -Name "Test Message" -Message "This is a test message" -Logname "C:\Logs\Test.log"
Message will be displayed only in the log file :
Write-Log -Type INFO -Name "Test Message" -Message "This is a test message" -Logname "C:\Logs\Test.log" -NotDisplay
.INPUTS
String
.OUTPUTS
Files with '.log' extention
.NOTES
Author: @Zardrilokis => [email protected]
Linked to: All customs functions and the entire scripts
#>
Param (
[Parameter(Mandatory=$True)]
[ValidateSet('INFO','INFONO','VALUE','WARNING','ERROR','DEBUG')]
[String]$Type,
[Parameter(Mandatory=$True)]
[ValidateSet('Program initialisation','Program Run','Program Stop')]
[String]$Category,
[Parameter(Mandatory=$True)]
[String]$Name,
[Parameter(Mandatory=$True)]
[String]$Message,
[Parameter(Mandatory=$False)]
[switch]$NotDisplay,
[Parameter(Mandatory=$False)]
[String]$Logname = "$global:LogDateFolderNamePath\$global:LogFileName"
)
$LogPath = $Logname + '.csv'
# Create log object
$log = [pscustomobject] @{Date=(Get-Date -UFormat %Y%m%d_%H%M%S) ; PID=$PID ; user= $(whoami) ; Type=$Type ; Category=$Category ; Name=$Name ; Message=$Message}
$log | Add-Member -Name ToString -MemberType ScriptMethod -value {$this.Date + ' : ' + $this.Type + ' : ' + $this.Category + ' : ' + $this.Name + ' : ' + $this.Message} -Force
# Append to global journal
[Object[]] $Global:journal += $log.toString()
If (-not $NotDisplay) {
Switch ($Type) {
'INFO' {Write-Host -Object "$Message" -ForegroundColor Cyan;Break}
'INFONO' {Write-Host -Object "$Message" -ForegroundColor Cyan -NoNewline;Break}
'VALUE' {Write-Host -Object "$Message" -ForegroundColor Green;Break}
'WARNING' {Write-Host -Object "$Message" -ForegroundColor Yellow;Break}
'ERROR' {Write-Host -Object "$Message" -ForegroundColor Red;Break}
'DEBUG' {Write-Host -Object "$Message" -ForegroundColor Blue;Break}
}
}
# Create or open Mutex
Try {
$mtx = [System.Threading.Mutex]::OpenExisting('Global\PegaseMutex')
}
Catch {
$mtx = New-Object System.Threading.Mutex($false,'Global\PegaseMutex')
}
Try {
$mtx.WaitOne() | Out-Null
# Write Header if file don't exists yet
If (-not (Test-Path $LogPath)) {
Out-File -FilePath $LogPath -Encoding unicode -Append -InputObject "Date;PID;Computer-User;Type;Category;Name;Message"
}
Out-File -FilePath $LogPath -Encoding unicode -Append -InputObject "$($Log.date);$($Log.pid);$($Log.user);$($Log.type);$($Log.Category);$($Log.name);$($Log.Message)"
}
Finally {
$mtx.ReleaseMutex()
}
}
#endregion Logs Files
#region Windows Credential Manager
# Install module TUN.CredentialManager
Function Install-TUNCredentialManager {
<#
.SYNOPSIS
To Install 'TUNCredentialManager' Module
.DESCRIPTION
To Install 'TUNCredentialManager' Module
.PARAMETER ModuleName
Use the Module Name without the version
.EXAMPLE
Install-TUNCredentialManager -ModuleName TUNCredentialManager
.INPUTS
'TUNCredentialManager' module from: https://www.powershellgallery.com
.OUTPUTS
Null
.NOTES
Author: @Zardrilokis => [email protected]
Linked to function(s): 'Stop-Program'
Linked to script(s): '.\Box-Administration.ps1'
Web Link: https://www.powershellgallery.com/packages/TUN.CredentialManager
#>
Param (
[Parameter(Mandatory=$True)]
[String]$ModuleName
)
Write-Log -Type INFONO -Category 'Program initialisation' -Name "Powershell $ModuleName Module installation" -Message "Powershell $ModuleName Module installation status : " -NotDisplay
If ($null -eq (Get-InstalledModule -name $ModuleName -ErrorAction SilentlyContinue)) {
Write-Log -Type WARNING -Category 'Program initialisation' -Name "Powershell $ModuleName Module installation" -Message 'Not yet' -NotDisplay
Write-Log -Type INFO -Category 'Program initialisation' -Name "Powershell $ModuleName Module installation" -Message "Try to install Powershell $ModuleName Module in user context" -NotDisplay
Write-Log -Type INFO -Category 'Program initialisation' -Name "Powershell $ModuleName Module installation" -Message "Powershell $ModuleName Module installation status : " -NotDisplay
Try {
Start-Process -FilePath Pwsh -Verb RunAs -WindowStyle Normal -Wait -ArgumentList {-ExecutionPolicy bypass -command "Install-Module -Name TUN.CredentialManager -Scope Allusers -verbose -Force -ErrorAction Stop;Pause"} -ErrorAction Stop
Start-Sleep -Seconds $global:SleepTUNCredentialManagerModuleinstallation
Write-Log -Type VALUE -Category 'Program initialisation' -Name "Powershell $ModuleName Module installation" -Message 'Successful' -NotDisplay
}
Catch {
Write-Log -Type WARNING -Category 'Program initialisation' -Name "Powershell $ModuleName Module installation" -Message "Failed, due to $($_.ToString())" -NotDisplay
Stop-Program -Context System -ErrorAction Stop
}
If (($null -eq $global:TriggerExitSystem) -and (Get-InstalledModule -name $ModuleName)) {
Write-Log -Type VALUE -Category 'Program initialisation' -Name "Powershell $ModuleName Module installation" -Message 'Successful' -NotDisplay
Write-Log -Type INFONO -Category 'Program initialisation' -Name "Powershell $ModuleName Module Importation" -Message "Powershell $ModuleName Module Importation status : " -NotDisplay
Try {
Import-Module $ModuleName -Global -Force -ErrorAction Stop
Write-Log -Type VALUE -Category 'Program initialisation' -Name "Powershell $ModuleName Module Importation" -Message 'Successful' -NotDisplay
}
Catch {
Write-Log -Type WARNING -Category 'Program initialisation' -Name "Powershell $ModuleName Module Importation" -Message "Failed, due to $($_.ToString())" -NotDisplay
Stop-Program -Context System -ErrorAction Stop
}
}
Else {
Write-Log -Type WARNING -Category 'Program initialisation' -Name "Powershell $ModuleName Module installation" -Message "Failed, due to $($_.ToString())" -NotDisplay
}
}
Else {
Write-Log -Type VALUE -Category 'Program initialisation' -Name "Powershell $ModuleName Module installation" -Message 'Already installed' -NotDisplay
}
}
# Unistall module TUN.CredentialManager
Function Uninstall-TUNCredentialManager {
<#
.SYNOPSIS
To Uninstall 'TUNCredentialManager' Module
.DESCRIPTION
To Uninstall 'TUNCredentialManager' Module
.PARAMETER ModuleName
Use the Module Name without the version
.EXAMPLE
Uninstall-TUNCredentialManager -ModuleName TUNCredentialManager
.INPUTS
'TUNCredentialManager' module from: https://www.powershellgallery.com
.OUTPUTS
Null
.NOTES
Author: @Zardrilokis => [email protected]
Linked to function(s): 'Stop-Program'
Linked to script(s): '.\Box-Administration.ps1'
Web Link: https://www.powershellgallery.com/packages/TUN.CredentialManager
#>
Param (
[Parameter(Mandatory=$True)]
[String]$ModuleName
)
Write-Log -Type INFONO -Category 'Program initialisation' -Name "Powershell $ModuleName Module uninstallation" -Message "Powershell $ModuleName Module uninstallation status : " -NotDisplay
If ($null -eq (Get-InstalledModule -name $ModuleName -ErrorAction SilentlyContinue)) {
Write-Log -Type WARNING -Category 'Program initialisation' -Name "Powershell $ModuleName Module uninstallation" -Message 'Not yet' -NotDisplay
Write-Log -Type INFO -Category 'Program initialisation' -Name "Powershell $ModuleName Module uninstallation" -Message "Try to install Powershell $ModuleName Module in user context" -NotDisplay
Write-Log -Type INFO -Category 'Program initialisation' -Name "Powershell $ModuleName Module uninstallation" -Message "Powershell $ModuleName Module uninstallation status : " -NotDisplay
Try {
Start-Process -FilePath Pwsh -Verb RunAs -WindowStyle Normal -Wait -ArgumentList {-ExecutionPolicy bypass -command "Uninstall-Module -Name TUN.CredentialManager -confirm:$false -verbose -Force -ErrorAction Stop;Pause"} -ErrorAction Stop
Start-Sleep -Seconds $global:SleepTUNCredentialManagerModuleinstallation
Write-Log -Type VALUE -Category 'Program initialisation' -Name "Powershell $ModuleName Module uninstallation" -Message 'Successful' -NotDisplay
}
Catch {
Write-Log -Type WARNING -Category 'Program initialisation' -Name "Powershell $ModuleName Module uninstallation" -Message "Failed, due to $($_.ToString())" -NotDisplay
Stop-Program -Context System -ErrorAction Stop
}
If (($null -eq $global:TriggerExitSystem) -and ($null -eq $(Get-InstalledModule -name $ModuleName))) {
Write-Log -Type VALUE -Category 'Program initialisation' -Name "Powershell $ModuleName Module uninstallation" -Message 'Successful' -NotDisplay
}
Else {
Write-Log -Type WARNING -Category 'Program initialisation' -Name "Powershell $ModuleName Module uninstallation" -Message "Failed, due to $($_.ToString())" -NotDisplay
}
}
Else {
Write-Log -Type VALUE -Category 'Program initialisation' -Name "Powershell $ModuleName Module uninstallation" -Message 'Already uninstalled' -NotDisplay
}
}
# Remove Box Credential stored in Windows Credential Manager
Function Remove-BoxCredential {
<#
.SYNOPSIS
To remove Box Credential set to the Windows Credential Manager
.DESCRIPTION
To remove Box Credential set to the Windows Credential Manager
.PARAMETER
.EXAMPLE
Remove-BoxCredential
.INPUTS
Null
.OUTPUTS
Null
.NOTES
Author: @Zardrilokis => [email protected]
Linked to function(s): 'Remove-StoredCredential'
#>
Param ()
$Name = 'Remove Box Credential'
$Category = 'Program run'
Write-Log -Type INFO -Category $Category -Name $Name -Message "Start $Name" -NotDisplay
Write-Log -Type INFONO -Category $Category -Name $Name -Message "$Name status : " -NotDisplay
Try {
$null = Remove-StoredCredential -Target $global:CredentialsTarget -ErrorAction Stop
Write-Log -Type VALUE -Category $Category -Name $Name -Message 'Successful' -NotDisplay
}
Catch {
Write-Log -Type WARNING -Category $Category -Name $Name -Message "Failed, due to : $($_.ToString())" -NotDisplay
}
Write-Log -Type INFO -Category $Category -Name $Name -Message "End $Name" -NotDisplay
}
# Show Box Credential stored in Windows Credential Manager
Function Show-BoxCredential {
<#
.SYNOPSIS
To display Box Credential stored in the Windows Credential Manager to Standard System Windows Forms MessageBox
.DESCRIPTION
To display Box Credential stored in the Windows Credential Manager to Standard System Windows Forms MessageBox
.PARAMETER
.EXAMPLE
Show-BoxCredential
.INPUTS
Credentials from Windows Credential Manager
.OUTPUTS
Standard System Windows Forms MessageBox
.NOTES
Author: @Zardrilokis => [email protected]
Linked to function(s): 'Get-StoredCredential', 'Show-WindowsFormDialogBox'
#>
Param ()
$Name = 'Show Box Credential'
$Category = 'Program run'
Write-Log -Type INFO -Category $Category -Name $Name -Message "Start $Name" -NotDisplay
Write-Log -Type INFONO -Category $Category -Name $Name -Message "$Name status : " -NotDisplay
Try {
$Password = $(Get-StoredCredential -Target $global:CredentialsTarget | Select-Object -Property Password).password
If ($Password) {
$Password = $Password | ConvertFrom-SecureString -AsPlainText
Write-Log -Type VALUE -Category $Category -Name $Name -Message 'Successful' -NotDisplay
Write-Log -Type INFONO -Category $Category -Name $Name -Message "Actual $global:BoxType Stored Password : **********" -NotDisplay
}
Else {
$Password = 'None password was found, please set it, before to show it'
Write-Log -Type VALUE -Category $Category -Name $Name -Message $Password -NotDisplay
}
$null = Show-WindowsFormDialogBox -Title "$Category - $Name" -Message "Actual $global:BoxType stored Password in Windows Credential Manager is : $Password" -InfoIcon
Clear-Variable -Name Password
}
Catch {
Write-Log -Type WARNING -Category $Category -Name $Name -Message "Failed, due to : $($_.ToString())" -NotDisplay
}
Write-Log -Type INFO -Category $Category -Name $Name -Message "Start $Name" -NotDisplay
}
# Add Box Credential in Windows Credential Manager
function Add-BoxCredential {
<#
.SYNOPSIS
Add Box Credential in Windows Credential Manager
.DESCRIPTION
Add Box Credential in Windows Credential Manager
.PARAMETER
.EXAMPLE
Add-BoxCredential
.INPUTS
$Credential
.OUTPUTS
Standard System Windows Forms MessageBox
Credentials stored in Windows Credential Manager
.NOTES
Author: @Zardrilokis => [email protected]
Linked to function(s): 'Show-BoxCredential'
#>
Param ()
$Credential = $null
$Credentialbuild = $null
$Name = 'Password Status'
$Category = 'Program run'
Write-Log -Type INFO -Category $Category -Name $Name -Message 'Asking password to the user ...' -NotDisplay
While ([String]::IsNullOrEmpty($Credential.Password) -or [String]::IsNullOrEmpty($Credential.UserName)) {
# Ask user to provide Box Web Interface Password
$Credential = Get-Credential -Message "Please enter your $global:BoxType Admin password use for the web portal interface. It will store securly in Windows Credential Manager to be used in future" -UserName $global:CredentialsTarget
}
Write-Log -Type INFO -Category $Category -Name $Name -Message 'Set new password to Windows Credential Manager in progress ...' -NotDisplay
Write-Log -Type INFONO -Category $Category -Name $Name -Message 'Windows Credential Manager status : ' -NotDisplay
Try {
$Comment = $global:CredentialsComment + " - Last modification : $(Get-Date -Format yyyyMMdd-HHmmss) - By : $(whoami)"
$Password = $Credential.Password | ConvertFrom-SecureString -AsPlainText
$Credentialbuild = New-StoredCredential -Target $global:CredentialsTarget -UserName $global:CredentialsUserName -Password $Password -Comment $Comment -Type Generic -Persist Session | Out-Null
Write-Log -Type VALUE -Category $Category -Name $Name -Message "Set - $Comment" -NotDisplay
Clear-Variable -Name Password
}
Catch {
Write-Log -Type WARNING -Category $Category -Name $Name -Message "Failed, due to : $($_.ToString())" -NotDisplay
}
Show-BoxCredential
Return $Credentialbuild
Clear-Variable -Name Credentialbuild
}
# Get Box Credential stored in Windows Credential Manager
Function Get-BoxCredential {
<#
.SYNOPSIS
To display Box Credential stored in the Windows Credential Manager to Standard System Windows Forms MessageBox
.DESCRIPTION
To display Box Credential stored in the Windows Credential Manager to Standard System Windows Forms MessageBox
.PARAMETER
.EXAMPLE
Get-BoxCredential
.INPUTS
Credentials from Windows Credential Manager
.OUTPUTS
Standard System Windows Forms MessageBox
.NOTES
Author: @Zardrilokis => [email protected]
Linked to function(s): 'Get-StoredCredential'
#>
Param ()
$Name = 'Get Box Credential'
$Category = 'Program run'
Write-Log -Type INFO -Category $Category -Name $Name -Message "Start $Name" -NotDisplay
Write-Log -Type INFONO -Category $Category -Name $Name -Message "$Name status : " -NotDisplay
Try {
$Password = Get-StoredCredential -Type 'Generic' -AsCredentialObject
$Password = $Password | Where-Object {($_.UserName -match "Admin") -and ($null -eq $_.SecurePassword) -and ($_.persist -eq 'session')}
If ($Password) {
Write-Log -Type VALUE -Category $Category -Name $Name -Message 'Successful' -NotDisplay
$Password | Select-Object -Property UserName,TargetName,Comment,LastWritten,Persist,Password | Out-GridView -Title "Admin Box Password Stored in Windows Credantial Management :" -Wait
}
Else {
$Password = 'None password was found, please set it, before to Get it'
Write-Log -Type VALUE -Category $Category -Name $Name -Message $Password
$null = Show-WindowsFormDialogBox -Title "$Category - $Name" -Message "Actual box Password stored in Windows Credential Manager : $Password" -InfoIcon
}
}
Catch {
Write-Log -Type WARNING -Category $Category -Name $Name -Message "Failed, due to : $($_.ToString())"
}
Write-Log -Type INFO -Category $Category -Name $Name -Message "Start $Name" -NotDisplay
}
#endregion Windows Credential Manager
#region Windows Form Dialog Box
#region User Input Box
# Used only to get user's 1 input
function Show-WindowsFormDialogBoxInput {
<#
.SYNOPSIS
To display a Standard System Windows Forms MessageBox with user input
.DESCRIPTION
To display a Standard System Windows Forms MessageBox with user input
.PARAMETER LabelMessageText
Text to display in the System Windows Forms MessageBox before user input field
.PARAMETER MainFormTitle
This is the text display in the header of the message box
.PARAMETER OkButtonText
Text to display to validate user input
.PARAMETER CancelButtonText
Text to display to cancel user input
.PARAMETER DefaultValue
Value set by default in the input field
.EXAMPLE
Show-WindowsFormDialogBoxInput -MainFormTitle "This is my Window Header text" -LabelMessageText "This is the body text " -OkButtonText "OK" -CancelButtonText "Cancel"
.INPUTS
$MainFormTitle
$LabelMessageText
$OkButtonText
$CancelButtonText
.OUTPUTS
Standard System Windows Forms MessageBox with user input
.NOTES
Author: @Zardrilokis => [email protected]
Linked to function(s): 'Get-HostStatus', 'Get-PortStatus'
#>
Param (
[Parameter(Mandatory=$True)]
[String]$MainFormTitle,
[Parameter(Mandatory=$True)]
[String]$LabelMessageText,
[Parameter(Mandatory=$True)]
[String]$OkButtonText,
[Parameter(Mandatory=$True)]
[String]$CancelButtonText,
[Parameter(Mandatory=$False)]
[String]$DefaultValue
)
$MainFormSizeX = 330
$MainFormSizeY = 250
$LabelMessageSizeX = 300
$LabelMessageSizeY = 80
$TextBoxSizeX = 100
$TextBoxSizeY = 40
$OkButtonSizeX = 75
$OkButtonSizeY = 25
$CancelButtonSizeX = 75
$CancelButtonSizeY = 25
$LabelMessageLocationX = 20
$LabelMessageLocationY = 40
$TextBoxLocationX = 20
$TextBoxLocationY = $LabelMessageLocationY + $LabelMessageSizeY
$OkButtonLocationX = 85
$OkButtonLocationY = $TextBoxLocationY + $TextBoxSizeY
$CancelButtonLocationX = $OkButtonLocationX + $OkButtonSizeX + 10
$CancelButtonLocationY = $TextBoxLocationY + $TextBoxSizeY
$MainForm = New-Object System.Windows.Forms.Form
$MainForm.Text = $MainFormTitle
$MainForm.Size = New-Object System.Drawing.Size($MainFormSizeX,$MainFormSizeY)
$MainForm.StartPosition = 'CenterScreen'
$LabelMessage = New-Object System.Windows.Forms.Label
$LabelMessage.Location = New-Object System.Drawing.Point($LabelMessageLocationX,$LabelMessageLocationY)
$LabelMessage.Size = New-Object System.Drawing.Size($LabelMessageSizeX,$LabelMessageSizeY)
$LabelMessage.Text = $LabelMessageText
$MainForm.Controls.Add($LabelMessage)
$TextBox = New-Object System.Windows.Forms.TextBox
$TextBox.Location = New-Object System.Drawing.Point($TextBoxLocationX,$TextBoxLocationY)
$TextBox.Size = New-Object System.Drawing.Size($TextBoxSizeX,$TextBoxSizeY)
$TextBox.Text = $DefaultValue # Prefield value
$MainForm.Controls.Add($TextBox)
$OkButton = New-Object System.Windows.Forms.Button
$OkButton.Location = New-Object System.Drawing.Point($OkButtonLocationX,$OkButtonLocationY)
$OkButton.Size = New-Object System.Drawing.Size($OkButtonSizeX,$OkButtonSizeY)
$OkButton.Text = $OkButtonText
$OkButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$MainForm.AcceptButton = $OkButton
$MainForm.Controls.Add($OkButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point($CancelButtonLocationX,$CancelButtonLocationY)
$CancelButton.Size = New-Object System.Drawing.Size($CancelButtonSizeX,$CancelButtonSizeY)
$CancelButton.Text = $CancelButtonText
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$MainForm.CancelButton = $CancelButton
$MainForm.Controls.Add($CancelButton)
$MainForm.Topmost = $true
$MainForm.Add_Shown({$TextBox.Select()})
If ($MainForm.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
Return $TextBox.Text
}
Else {
$global:TriggerDialogBox = 1
}
}
# Used only to get user's 5 inputs
function Show-WindowsFormDialogBox8Inuput {
<#
.SYNOPSIS
To display a Standard System Windows Forms MessageBox with user inputs
.DESCRIPTION
To display a Standard System Windows Forms MessageBox with user inputs
.PARAMETER LabelMessageTextx
Text to display in the System Windows Forms MessageBox before user input field
.PARAMETER DefaultValuex
Value set by default in the input field
.PARAMETER MainFormTitle
This is the text display in the header of the message box
.PARAMETER OkButtonText
Text to display to validate user input
.PARAMETER CancelButtonText
Text to display to cancel user input
.EXAMPLE
Show-WindowsFormDialogBox8Inuput -MainFormTitle "Please complete this form :" -LabelMessageText0 "PhoneNumber :" -DefaultValue0 "+33102030405" -LabelMessageText1 "Prefixe :" -DefaultValue1 "+33" -LabelMessageText2 "Number :" -DefaultValue2 "0102030405" -LabelMessageText3 "Name :" -DefaultValue3 "DUPONT" -LabelMessageText4 "SurName :" -DefaultValue4 "Dupont" -LabelMessageText5 "Description :" -DefaultValue5 "This is the desciption of the contact" -LabelMessageText6 "Category :" -DefaultValue6 "Family / Friend / Others" -LabelMessageText7 "Type :" -DefaultValue7 "Mobile / Fixe" -OkButtonText "OK" -CancelButtonText "Cancel"
.INPUTS
$MainFormTitle
$LabelMessageText0
$LabelMessageText1
$LabelMessageText2
$LabelMessageText3
$LabelMessageText4
$LabelMessageText5
$LabelMessageText6
$LabelMessageText7
$DefaultValue0
$DefaultValue1
$DefaultValue2
$DefaultValue3
$DefaultValue4
$DefaultValue5
$DefaultValue6
$DefaultValue7
$OkButtonText
$CancelButtonText
.OUTPUTS
Standard System Windows Forms MessageBox with user inputs
.NOTES
Author: @Zardrilokis => [email protected]
Linked to function(s): 'Add-NewReferentialContact'
#>
Param (
[Parameter(Mandatory=$True)]
[String]$MainFormTitle,
[Parameter(Mandatory=$True)]
[String]$LabelMessageText0,
[Parameter(Mandatory=$False)]
[String]$DefaultValue0,
[Parameter(Mandatory=$True)]
[String]$LabelMessageText1,
[Parameter(Mandatory=$False)]
[String]$DefaultValue1,
[Parameter(Mandatory=$True)]
[String]$LabelMessageText2,
[Parameter(Mandatory=$False)]
[String]$DefaultValue2,
[Parameter(Mandatory=$True)]
[String]$LabelMessageText3,
[Parameter(Mandatory=$False)]
[String]$DefaultValue3,
[Parameter(Mandatory=$True)]
[String]$LabelMessageText4,
[Parameter(Mandatory=$False)]
[String]$DefaultValue4,
[Parameter(Mandatory=$True)]
[String]$LabelMessageText5,
[Parameter(Mandatory=$False)]
[String]$DefaultValue5,
[Parameter(Mandatory=$True)]
[String]$LabelMessageText6,
[Parameter(Mandatory=$False)]
[String]$DefaultValue6,
[Parameter(Mandatory=$True)]
[String]$LabelMessageText7,
[Parameter(Mandatory=$False)]
[String]$DefaultValue7,
[Parameter(Mandatory=$True)]
[String]$OkButtonText,
[Parameter(Mandatory=$True)]
[String]$CancelButtonText
)
$MainFormSizeX = 260
$MainFormSizeY = 600
$LabelMessageSizeX = 300
$LabelMessageSizeY = 30
$TextBoxSizeX = 200
$TextBoxSizeY = 40
$OkButtonSizeX = 75
$OkButtonSizeY = 25
$CancelButtonSizeX = 75
$CancelButtonSizeY = 25
$LabelMessage0LocationX = 20
$LabelMessage0LocationY = 20
$TextBox0LocationX = $LabelMessage0LocationX
$TextBox0LocationY = $LabelMessage0LocationY + $LabelMessageSizeY
$LabelMessage1LocationX = $LabelMessage0LocationX
$LabelMessage1LocationY = $TextBox0LocationY + $LabelMessageSizeY
$TextBox1LocationX = $LabelMessage0LocationX
$TextBox1LocationY = $LabelMessage1LocationY + $LabelMessageSizeY
$LabelMessage2LocationX = $LabelMessage0LocationX
$LabelMessage2LocationY = $TextBox1LocationY + $LabelMessageSizeY
$TextBox2LocationX = $LabelMessage0LocationX
$TextBox2LocationY = $LabelMessage2LocationY + $LabelMessageSizeY
$LabelMessage3LocationX = $LabelMessage0LocationX
$LabelMessage3LocationY = $TextBox2LocationY + $LabelMessageSizeY
$TextBox3LocationX = $LabelMessage0LocationX
$TextBox3LocationY = $LabelMessage3LocationY + $LabelMessageSizeY
$LabelMessage4LocationX = $LabelMessage0LocationX
$LabelMessage4LocationY = $TextBox3LocationY + $LabelMessageSizeY
$TextBox4LocationX = $LabelMessage0LocationX
$TextBox4LocationY = $LabelMessage4LocationY + $LabelMessageSizeY
$LabelMessage5LocationX = $LabelMessage0LocationX
$LabelMessage5LocationY = $TextBox4LocationY + $LabelMessageSizeY
$TextBox5LocationX = $LabelMessage0LocationX
$TextBox5LocationY = $LabelMessage5LocationY + $LabelMessageSizeY
$LabelMessage6LocationX = $LabelMessage0LocationX
$LabelMessage6LocationY = $TextBox5LocationY + $LabelMessageSizeY
$TextBox6LocationX = $LabelMessage0LocationX
$TextBox6LocationY = $LabelMessage6LocationY + $LabelMessageSizeY
$LabelMessage7LocationX = $LabelMessage0LocationX
$LabelMessage7LocationY = $TextBox6LocationY + $LabelMessageSizeY
$TextBox7LocationX = $LabelMessage0LocationX
$TextBox7LocationY = $LabelMessage7LocationY + $LabelMessageSizeY
$OkButtonLocationX = $LabelMessage0LocationX
$OkButtonLocationY = $TextBox7LocationY + $TextBoxSizeY
$CancelButtonLocationX = $OkButtonLocationX + $OkButtonSizeX + 10
$CancelButtonLocationY = $TextBox7LocationY + $TextBoxSizeY
$MainForm = New-Object System.Windows.Forms.Form
$MainForm.Text = $MainFormTitle
$MainForm.Size = New-Object System.Drawing.Size($MainFormSizeX,$MainFormSizeY)
$MainForm.StartPosition = 'CenterScreen'
$LabelMessage0 = New-Object System.Windows.Forms.Label
$LabelMessage0.Location = New-Object System.Drawing.Point($LabelMessage0LocationX,$LabelMessage0LocationY)
$LabelMessage0.Size = New-Object System.Drawing.Size($LabelMessageSizeX,$LabelMessageSizeY)
$LabelMessage0.Text = $LabelMessageText0
$MainForm.Controls.Add($LabelMessage0)
$TextBox0 = New-Object System.Windows.Forms.TextBox
$TextBox0.Location = New-Object System.Drawing.Point($TextBox0LocationX,$TextBox0LocationY)
$TextBox0.Size = New-Object System.Drawing.Size($TextBoxSizeX,$TextBoxSizeY)
$TextBox0.Text = $DefaultValue0 # Prefield value
$MainForm.Controls.Add($TextBox0)
$LabelMessage1 = New-Object System.Windows.Forms.Label
$LabelMessage1.Location = New-Object System.Drawing.Point($LabelMessage1LocationX,$LabelMessage1LocationY)
$LabelMessage1.Size = New-Object System.Drawing.Size($LabelMessageSizeX,$LabelMessageSizeY)
$LabelMessage1.Text = $LabelMessageText1
$MainForm.Controls.Add($LabelMessage1)
$TextBox1 = New-Object System.Windows.Forms.TextBox
$TextBox1.Location = New-Object System.Drawing.Point($TextBox1LocationX,$TextBox1LocationY)
$TextBox1.Size = New-Object System.Drawing.Size($TextBoxSizeX,$TextBoxSizeY)
$TextBox1.Text = $DefaultValue1 # Prefield value
$MainForm.Controls.Add($TextBox1)
$LabelMessage2 = New-Object System.Windows.Forms.Label
$LabelMessage2.Location = New-Object System.Drawing.Point($LabelMessage2LocationX,$LabelMessage2LocationY)
$LabelMessage2.Size = New-Object System.Drawing.Size($LabelMessageSizeX,$LabelMessageSizeY)
$LabelMessage2.Text = $LabelMessageText2
$MainForm.Controls.Add($LabelMessage2)
$TextBox2 = New-Object System.Windows.Forms.TextBox
$TextBox2.Location = New-Object System.Drawing.Point($TextBox2LocationX,$TextBox2LocationY)
$TextBox2.Size = New-Object System.Drawing.Size($TextBoxSizeX,$TextBoxSizeY)
$TextBox2.Text = $DefaultValue2 # Prefield value
$MainForm.Controls.Add($TextBox2)
$LabelMessage3 = New-Object System.Windows.Forms.Label
$LabelMessage3.Location = New-Object System.Drawing.Point($LabelMessage3LocationX,$LabelMessage3LocationY)
$LabelMessage3.Size = New-Object System.Drawing.Size($LabelMessageSizeX,$LabelMessageSizeY)
$LabelMessage3.Text = $LabelMessageText3
$MainForm.Controls.Add($LabelMessage3)
$TextBox3 = New-Object System.Windows.Forms.TextBox
$TextBox3.Location = New-Object System.Drawing.Point($TextBox3LocationX,$TextBox3LocationY)
$TextBox3.Size = New-Object System.Drawing.Size($TextBoxSizeX,$TextBoxSizeY)
$TextBox3.Text = $DefaultValue3 # Prefield value
$MainForm.Controls.Add($TextBox3)
$LabelMessage4 = New-Object System.Windows.Forms.Label
$LabelMessage4.Location = New-Object System.Drawing.Point($LabelMessage4LocationX,$LabelMessage4LocationY)
$LabelMessage4.Size = New-Object System.Drawing.Size($LabelMessageSizeX,$LabelMessageSizeY)
$LabelMessage4.Text = $LabelMessageText4
$MainForm.Controls.Add($LabelMessage4)
$TextBox4 = New-Object System.Windows.Forms.TextBox
$TextBox4.Location = New-Object System.Drawing.Point($TextBox4LocationX,$TextBox4LocationY)
$TextBox4.Size = New-Object System.Drawing.Size($TextBoxSizeX,$TextBoxSizeY)
$TextBox4.Text = $DefaultValue4 # Prefield value
$MainForm.Controls.Add($TextBox4)
$LabelMessage5 = New-Object System.Windows.Forms.Label
$LabelMessage5.Location = New-Object System.Drawing.Point($LabelMessage5LocationX,$LabelMessage5LocationY)
$LabelMessage5.Size = New-Object System.Drawing.Size($LabelMessageSizeX,$LabelMessageSizeY)
$LabelMessage5.Text = $LabelMessageText5
$MainForm.Controls.Add($LabelMessage5)
$TextBox5 = New-Object System.Windows.Forms.TextBox
$TextBox5.Location = New-Object System.Drawing.Point($TextBox5LocationX,$TextBox5LocationY)
$TextBox5.Size = New-Object System.Drawing.Size($TextBoxSizeX,$TextBoxSizeY)
$TextBox5.Text = $DefaultValue5 # Prefield value
$MainForm.Controls.Add($TextBox5)
$LabelMessage6 = New-Object System.Windows.Forms.Label
$LabelMessage6.Location = New-Object System.Drawing.Point($LabelMessage6LocationX,$LabelMessage6LocationY)
$LabelMessage6.Size = New-Object System.Drawing.Size($LabelMessageSizeX,$LabelMessageSizeY)
$LabelMessage6.Text = $LabelMessageText6
$MainForm.Controls.Add($LabelMessage6)
$TextBox6 = New-Object System.Windows.Forms.TextBox
$TextBox6.Location = New-Object System.Drawing.Point($TextBox6LocationX,$TextBox6LocationY)
$TextBox6.Size = New-Object System.Drawing.Size($TextBoxSizeX,$TextBoxSizeY)
$TextBox6.Text = $DefaultValue6 # Prefield value
$MainForm.Controls.Add($TextBox6)
$LabelMessage7 = New-Object System.Windows.Forms.Label
$LabelMessage7.Location = New-Object System.Drawing.Point($LabelMessage7LocationX,$LabelMessage7LocationY)
$LabelMessage7.Size = New-Object System.Drawing.Size($LabelMessageSizeX,$LabelMessageSizeY)
$LabelMessage7.Text = $LabelMessageText7
$MainForm.Controls.Add($LabelMessage7)
$TextBox7 = New-Object System.Windows.Forms.TextBox
$TextBox7.Location = New-Object System.Drawing.Point($TextBox7LocationX,$TextBox7LocationY)
$TextBox7.Size = New-Object System.Drawing.Size($TextBoxSizeX,$TextBoxSizeY)
$TextBox7.Text = $DefaultValue7 # Prefield value
$MainForm.Controls.Add($TextBox7)
$OkButton = New-Object System.Windows.Forms.Button
$OkButton.Location = New-Object System.Drawing.Point($OkButtonLocationX,$OkButtonLocationY)
$OkButton.Size = New-Object System.Drawing.Size($OkButtonSizeX,$OkButtonSizeY)
$OkButton.Text = $OkButtonText
$OkButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$MainForm.AcceptButton = $OkButton
$MainForm.Controls.Add($OkButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point($CancelButtonLocationX,$CancelButtonLocationY)
$CancelButton.Size = New-Object System.Drawing.Size($CancelButtonSizeX,$CancelButtonSizeY)
$CancelButton.Text = $CancelButtonText
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$MainForm.CancelButton = $CancelButton
$MainForm.Controls.Add($CancelButton)
$MainForm.Topmost = $true
$MainForm.Add_Shown({$TextBox0.Select()})
$MainForm.Add_Shown({$TextBox1.Select()})
$MainForm.Add_Shown({$TextBox2.Select()})
$MainForm.Add_Shown({$TextBox3.Select()})
$MainForm.Add_Shown({$TextBox4.Select()})
$MainForm.Add_Shown({$TextBox5.Select()})
$MainForm.Add_Shown({$TextBox6.Select()})
$MainForm.Add_Shown({$TextBox7.Select()})
If ($MainForm.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
Return [PSCustomObject]@{
PhoneNumber = $TextBox0.Text
Prefixe = $TextBox1.Text