我一直在研究一种脚本,该脚本会生成一个密码,该密码具有特定长度的特定字符,但具有特定长度的随机数字。
该脚本具有一个GUI(正在进行中,我将最终完成它)。
我面临的问题是,每当我按下“生成密码”时,它都会创建一个密码,但是在生成密码后却没有给我一个新的密码。它只提供与第一次生成时相同的密码。
每次在按下按钮时,我都在网上寻找如何获取新密码的方法,但是我什么都没找到。
有人可以提供一些提示吗?
谢谢。
脚本为:
Function Button_Click()
{
[System.Windows.Forms.MessageBox]::Show($DefinedLetters)
}
Function Generate-Form {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Build Form
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Password Generator"
$Form.Size = New-Object System.Drawing.Size(200,200)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
# Add Button
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Generate Password"
$Form.Controls.Add($Button)
#Add Button event
$Button.Add_Click({Button_Click})
#Show the Form
$form.ShowDialog()| Out-Null
} #End Function
# Password generator #
Function DefinedLetters
{
$DefinedLetters = 'Summer'
$numbers = 0..5
$array = @()
$array += $DefinedLetters.Split(',') | Get-Random -Count 4
$DefinedLetters += $numbers | Get-Random -Count 4
($DefinedLetters | Get-Random -Count $DefinedLetters.Count) -join ""
}
#Call the Function
Generate-Form
答案 0 :(得分:0)
在Button_Click()
中,您仅引用变量,而不调用函数。您需要将值形式函数分配给变量,例如:
Function Button_Click()
{
$PW = DefinedLetters
[System.Windows.Forms.MessageBox]::Show($PW)
}
答案 1 :(得分:0)
这不是生成密码的好方法,但这是您的代码版本,每次使用“ summer”(夏季)的4个字母和(0,1,2,3 ,4,5):
# Password generator #
Function DefinedLetters {
$DefinedLetters = 'Summer'
$numbers = 0..5
$array = @()
$array += $DefinedLetters.ToCharArray() | Get-Random -Count 4
$array += $numbers | Get-Random -Count 4
($array | Get-Random -Count $array.Count) -join ""
}
Function Button_Click() {
$DefinedLetters = DefinedLetters
[System.Windows.Forms.MessageBox]::Show($DefinedLetters)
}
Function Generate-Form {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Build Form
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Password Generator"
$Form.Size = New-Object System.Drawing.Size(200,200)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
# Add Button
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Generate Password"
$Form.Controls.Add($Button)
#Add Button event
$Button.Add_Click({Button_Click})
#Show the Form
$form.ShowDialog()| Out-Null
} #End Function
#Call the Function
Generate-Form
答案 2 :(得分:0)
Function Button_Click(){
[System.Windows.Forms.MessageBox]::Show((DefinedLetters))
}
Function Generate-Form {
#... snipped for brevity ...
}
Function DefinedLetters{
(([char[]]'Summer' | Get-Random -Count 4) -join '')+
((0..5| Get-Random -Count 4) -join '')
}
#Call the Function
Generate-Form