检查密码最小长度

时间:2017-08-27 13:52:33

标签: winforms powershell input textbox

我正在构建自己的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()

2 个答案:

答案 0 :(得分:4)

您的$pw_length包含string。对于-lt操作,您需要它为integer

解决方案:像这样投射你的变量。

[int]$pw_length = $NumberBox1.Text

$pw_length = $NumberBox1.Text -as [int]

答案 1 :(得分:1)

您可以使用[math] :: max函数,因此如果需要,则为no(内部强制转换):

$generate.Add_Click( {

    $pw_length = [math]::max($NumberBox1.Text,8)
    $NumberBox1.Text = $pw_length

    ## 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"
})