我正在構建我自己的PowerShell GUI密碼生成器,目前爲止它的工作原理。但是,當我想要更多地控制輸入時,我遇到了一個問題; 驗證最小長度輸入是否大於x。檢查密碼最小長度
$NumberBox1
是設置密碼長度的文本框。如果小於8,則應該自動將其設置爲8,並在單擊該按鈕時生成密碼。 如果我將文本框$NumberBox1
中的值更改爲9,則該值有效。但不是當值低於8或高於9
這是這一部分,我不能得到正確的:
## Check password length
if ($pw_length -le "7") {
$pw_length = "8"
$NumberBox1.Text = "8"
}
如果我註釋掉上面的部分,我可以設置密碼的長度。
全部下面的代碼:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$form1 = New-Object System.Windows.Forms.Form
$form1.Text = "Password generator"
$form1.Name = "Password generator"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 400
$System_Drawing_Size.Height = 200
$form1.ClientSize = $System_Drawing_Size
$NumberBox1 = New-Object System.Windows.Forms.TextBox
$NumberBox1.Location = New-Object System.Drawing.Size(110,20)
$NumberBox1.Size = New-Object System.Drawing.Size(50,25)
$NumberBox1.Text = "12"
$InfoBox1 = New-Object System.Windows.Forms.Label
$InfoBox1.Location = New-Object System.Drawing.Size(20,23)
$InfoBox1.Size = New-Object System.Drawing.Size(150,25)
$InfoBox1.Text = "Password length:"
$InfoBox3 = New-Object System.Windows.Forms.Label
$InfoBox3.Location = New-Object System.Drawing.Size(170,24)
$InfoBox3.Size = New-Object System.Drawing.Size(180,25)
$InfoBox3.Text = "Minimum length is always set to 8"
$TextBox1 = New-Object System.Windows.Forms.TextBox
$TextBox1.Location = New-Object System.Drawing.Size(200,140)
$TextBox1.Size = New-Object System.Drawing.Size(150,25)
$generate = New-Object System.Windows.Forms.Button
$generate.Location = New-Object System.Drawing.Size(20,140)
$generate.Size = New-Object System.Drawing.Size(150,22)
$generate.Text = "Generate password"
$InfoBox2 = New-Object System.Windows.Forms.Label
$InfoBox2.Location = New-Object System.Drawing.Size(20,165)
$InfoBox2.Size = New-Object System.Drawing.Size(360,25)
$generate.Add_Click({
$pw_length = $NumberBox1.Text
## Check password length
if ($pw_length -lt "7") {
$pw_length = "8"
$NumberBox1.Text = "8"
}
## Password with letters, numbers and punctiation
$PW_F = ([char[]](Get-Random -Input $(33..93 + 95 + 97..122) -Count $pw_length)) -join ""
$TextBox1.Text = $pw_F
$InfoBox2.Text = "Full complexity used. Length: $pw_length"
})
$form1.Controls.Add($generate)
$form1.Controls.Add($NumberBox1)
$form1.Controls.Add($TextBox1)
$form1.Controls.Add($InfoBox1)
$form1.Controls.Add($InfoBox2)
$form1.Controls.Add($InfoBox3)
$form1.Add_Shown({$form1.Activate()})
[void] $form1.ShowDialog()