每次单击时都会运行Powershell按钮

时间:2014-02-22 20:53:57

标签: powershell

每次单击按钮时,我都会尝试获取密码按钮以生成新密码。第一次点击生成一个密码,我的要求很好,但我需要它在每次点击重新运行。我知道它可能在add_click()内很简单,但我似乎无法找到任何东西。以下是我与按钮和密码生成有关的所有内容。

#form1: Button1 "password generate" 
$button1 = New-Object system.windows.forms.button
$button1.location = "10, 25"
$button1.size = "125, 35"
$button1.text = "Generate Password"
$button1.add_click($displayPassword)
$form1.controls.add($button1)

#######Password######

$caps = [char[]] "ABCDEFGHJKMNPQRSTUVWXY"
$lows = [char[]] "abcdefghjkmnpqrstuvwxy"
$nums = [char[]] "2346789"
$special = [char[]] "#%$+<=>?"

$first = Get-Random -Minimum 2 
$second = Get-Random -Minimum 2
$third = Get-Random -Miniumum 2 
$fourth = Get-Random -Minimum 2
$ofs = ""
$pwd = [string](@($caps | Get-Random -Count $first) + @($lows | Get-Random -Count $second) + @($nums | Get-Random -Count $third)+ @($special | Get-Random -Count $fourth) | Get-Random -Count 15)

$displayPassword = {$textbox1.text = "$pwd"}

1 个答案:

答案 0 :(得分:3)

用于计算随机密码的所有内容只运行一次。您的click eventhandler仅更新文本框,但它永远不会重新生成新密码。

尝试这样的事情:

#form1: Button1 "password generate" 
$button1 = New-Object system.windows.forms.button
$button1.location = "10, 25"
$button1.size = "125, 35"
$button1.text = "Generate Password"
$button1.add_click({
    $first = Get-Random -Minimum 2 
    $second = Get-Random -Minimum 2
    $third = Get-Random -Miniumum 2 
    $fourth = Get-Random -Minimum 2
    $pwd = [string](@($caps | Get-Random -Count $first) + @($lows | Get-Random -Count $second) + @($nums | Get-Random -Count $third)+ @($special | Get-Random -Count $fourth) | Get-Random -Count 15)
    $textbox1.text = "$pwd"

})

$form1.controls.add($button1)

#######Static Password Resources######

$caps = [char[]] "ABCDEFGHJKMNPQRSTUVWXY"
$lows = [char[]] "abcdefghjkmnpqrstuvwxy"
$nums = [char[]] "2346789"
$special = [char[]] "#%$+<=>?"
$ofs = ""

仅供参考,未来的问题包括完整且有效的样本。您的示例引用了永远不会声明的$form1$textbox1

相关问题