Functions/New-InputBoxSecureString.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
function New-InputBoxSecureString {
    <#
.SYNOPSIS
    Provides a GUI text entry box to enter a string and convert it to a securestring
.DESCRIPTION
    Provides a GUI text entry box to enter a string and convert it to a securestring. Function validates input and provides an input mask, both of which can be disabled.
.PARAMETER Title
    The title of the form. Defaults to 'New SecureString'
.PARAMETER DisableValidation
    Switch that will turn off the second entry box which ensures that data is input correctly.
.PARAMETER DisableInputMask
    Switch that will disable the input mask character which defaults to '*'
.PARAMETER Description
    The description block of the form. Defaults to 'Please enter the plain text you wish to be converted to a securestring'
.PARAMETER EntryBoxLabel
    The label block of the entry text input box. Defaults to 'Please enter the value'
.PARAMETER ValidateBoxLabel
    The label block of the entry text input box. Defaults to 'Please re-enter the value'
.EXAMPLE
    $SecureString = New-InputBoxSecureString
 
    Will display a form with 2 text entry fields that are masked with '*', and will verify the values match before converting to a securestring
.EXAMPLE
    $SecureString = New-InputBoxSecureString -Verbose
    VERBOSE: Starting [New-InputBoxSecureString]
    VERBOSE: Title [New SecureString]
    VERBOSE: Description [Please enter the plain text you wish to be converted to a securestring]
    VERBOSE: EntryBoxLabel [Please enter the value]
    VERBOSE: ValidateBoxLabel [Please re-enter the value]
    VERBOSE: DisableValidation [False], DisableInputMask [False]
    System.Security.SecureString
    VERBOSE: Ending [New-InputBoxSecureString]
 
    Will display a form with 2 text entry fields that are masked with '*', and will verify the values match before converting to a securestring
#>


    [CmdletBinding(DefaultParameterSetName = 'Default')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')]
    param (
        [Parameter(ParameterSetName = 'Default')]
        [Parameter(ParameterSetName = 'Validate')]
        [string] $Title = 'New SecureString',

        [Parameter(ParameterSetName = 'Validate')]
        [switch] $DisableValidation,

        [Parameter(ParameterSetName = 'Default')]
        [Parameter(ParameterSetName = 'Validate')]
        [switch] $DisableInputMask,

        [Parameter(ParameterSetName = 'Default')]
        [Parameter(ParameterSetName = 'Validate')]
        [string] $Description = 'Please enter the plain text you wish to be converted to a securestring',

        [Parameter(ParameterSetName = 'Default')]
        [Parameter(ParameterSetName = 'Validate')]
        [string] $EntryBoxLabel = 'Please enter the value',

        [Parameter(ParameterSetName = 'Validate')]
        [string] $ValidateBoxLabel = 'Please re-enter the value'
    )

    begin {
        Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
        Write-Verbose -Message "Title [$Title]"
        Write-Verbose -Message "Description [$Description]"
        Write-Verbose -Message "EntryBoxLabel [$EntryBoxLabel]"
        Write-Verbose -Message "ValidateBoxLabel [$ValidateBoxLabel]"
        Write-Verbose -Message "DisableValidation [$DisableValidation], DisableInputMask [$DisableInputMask]"
        Add-Type -AssemblyName 'System.Windows.Forms'
        Add-Type -AssemblyName 'System.Drawing'

        #~~< ValidateEntry >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $ValidateEntry = New-Object -TypeName System.Windows.Forms.Form
        $ValidateEntry.AutoSize = $true
        $ValidateEntry.ClientSize = New-Object -TypeName System.Drawing.Size -ArgumentList (300, 220)
        $ValidateEntry.Font = [System.Drawing.SystemFonts]::get_MessageBoxFont()
        $ValidateEntry.Text = $Title
        $ValidateEntry.AutoSize = $true
        $ValidateEntry.AutoSizeMode = 'GrowOnly'
        #~~< OKButton >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $OKButton = New-Object -TypeName System.Windows.Forms.Button
        $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
        $OKButton.Font = [System.Drawing.SystemFonts]::get_MessageBoxFont()
        $OKButton.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (197, 12)
        $OKButton.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (75, 23)
        $OKButton.TabIndex = 2
        $OKButton.Text = 'OK'
        $OKButton.UseVisualStyleBackColor = $true
        $ValidateEntry.AcceptButton = $OKButton
        #~~< CancelButton >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $CancelButton = New-Object -TypeName System.Windows.Forms.Button
        $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
        $CancelButton.Font = [System.Drawing.SystemFonts]::get_MessageBoxFont()
        $CancelButton.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (197, 60)
        $CancelButton.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (75, 23)
        $CancelButton.TabIndex = 3
        $CancelButton.Text = 'Cancel'
        $CancelButton.UseVisualStyleBackColor = $true
        $ValidateEntry.CancelButton = $CancelButton
        #~~< EntryLabel >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $EntryLabel = New-Object -TypeName System.Windows.Forms.Label
        $EntryLabel.Font = [System.Drawing.SystemFonts]::get_MessageBoxFont()
        $EntryLabel.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (12, 96)
        $EntryLabel.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (250, 39)
        $EntryLabel.Text = $EntryBoxLabel
        #~~< EntryBox >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $EntryBox = New-Object -TypeName System.Windows.Forms.TextBox
        $EntryBox.Font = [System.Drawing.SystemFonts]::get_MessageBoxFont()
        $EntryBox.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (12, 138)
        $EntryBox.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (260, 24)
        $EntryBox.TabIndex = 0
        if (-not $DisableInputMask) {
            $EntryBox.PasswordChar = '*'
        }
        $EntryBox.Text = ''
        if (-not $DisableValidation) {
            $ValidateEntry.ClientSize = New-Object -TypeName System.Drawing.Size -ArgumentList (300, 260)
            #~~< ValidateLabel >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            $ValidateLabel = New-Object -TypeName System.Windows.Forms.Label
            $ValidateLabel.Font = [System.Drawing.SystemFonts]::get_MessageBoxFont()
            $ValidateLabel.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (12, 175)
            $ValidateLabel.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (250, 53)
            #$ValidateLabel.TabIndex = 4
            $ValidateLabel.Text = $ValidateBoxLabel
            #~~< ValidateBox >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            $ValidateBox = New-Object -TypeName System.Windows.Forms.TextBox
            $ValidateBox.Font = [System.Drawing.SystemFonts]::get_MessageBoxFont()
            $ValidateBox.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (12, 221)
            $ValidateBox.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (260, 24)
            $ValidateBox.TabIndex = 1
            if (-not $DisableInputMask) {
                $ValidateBox.PasswordChar = '*'
            }
            $ValidateBox.Text = ''
        }
        #~~< DescriptionLabel >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $DescriptionLabel = New-Object -TypeName System.Windows.Forms.Label
        $DescriptionLabel.Font = [System.Drawing.SystemFonts]::get_MessageBoxFont()
        $DescriptionLabel.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (12, 12)
        $DescriptionLabel.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (180, 200)
        #$DescriptionLabel.TabIndex = 6
        $DescriptionLabel.Text = $Description
        if (-not $DisableValidation) {
            $ValidateEntry.Controls.Add($ValidateBox)
            $ValidateEntry.Controls.Add($ValidateLabel)
        }
        $ValidateEntry.Controls.Add($EntryBox)
        $ValidateEntry.Controls.Add($EntryLabel)
        $ValidateEntry.Controls.Add($DescriptionLabel)
        $ValidateEntry.Controls.Add($CancelButton)
        $ValidateEntry.Controls.Add($OKButton)

        #region myCustomFormCode
        $ValidateEntry.AutoSize = $true
        $ValidateEntry.AutoSizeMode = 'GrowOnly'
        $ValidateEntry.Font = [System.Drawing.SystemFonts]::get_MessageBoxFont()
        if ($Script:ModulePath) {
            $ValidateEntry.Icon = (Join-Path -Path $Script:ModulePath -ChildPath 'Resources\PoshFunctions.ico')
        }
        #endregion
    }

    process {
        [System.Windows.Forms.Application]::EnableVisualStyles()
        $Result = $ValidateEntry.ShowDialog()
        if ($Result -eq 'OK') {
            if (-not $DisableValidation) {
                if ($ValidateBox.Text -eq $EntryBox.Text) {
                    if ($EntryBox.Text.Length -eq 0) {
                        Write-Error -Message 'Must enter a string'
                        break
                    }
                } else {
                    Write-Error -Message 'Values do NOT match'
                    break
                }
            } else {
                   if ($EntryBox.Text.Length -eq 0) {
                        Write-Error -Message 'Must enter a string'
                        break
                    }
            }
            $ReturnVal = ConvertTo-SecureString -String $EntryBox.Text -AsPlainText -Force
            Write-Output -InputObject $ReturnVal
        }
    }

    end {
        Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
    }
}