Powershell脚本实现随机生成密码
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$form = New-Object System.Windows.Forms.Form
$form.Text = '随机密码生成器'
$form.Size = New-Object System.Drawing.Size(350, 300)
$form.StartPosition = 'CenterScreen'
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$labelLength = New-Object System.Windows.Forms.Label
$labelLength.Text = '密码长度:'
$labelLength.Location = New-Object System.Drawing.Point(10, 20)
$labelLength.Font = New-Object System.Drawing.Font("Consolas", 11)
$textBoxLength = New-Object System.Windows.Forms.TextBox
$textBoxLength.Location = New-Object System.Drawing.Point(110, 16)
$textBoxLength.Size = New-Object System.Drawing.Size(100, 20)
$textBoxLength.Font = New-Object System.Drawing.Font("Consolas", 12)
$textBoxPassword = New-Object System.Windows.Forms.TextBox
$textBoxPassword.Location = New-Object System.Drawing.Point(10, 170)
$textBoxPassword.Size = New-Object System.Drawing.Size(260, 20)
$textBoxPassword.ReadOnly = $true
$textBoxPassword.Font = New-Object System.Drawing.Font("Consolas", 12)
$yPos = 60
$checkboxes = @()
for ($i = 0; $i -lt 4; $i++) {
$checkBox = New-Object System.Windows.Forms.CheckBox
$checkBox.Text = @(
'包含数字'
'包含小写字母'
'包含大写字母'
'包含特殊字符'
)[$i]
$checkBox.Location = New-Object System.Drawing.Point(10, $yPos)
$checkBox.Checked = $true
$checkboxes += $checkBox
$form.Controls.Add($checkBox)
$yPos += 25
}
$buttonGenerate = New-Object System.Windows.Forms.Button
$buttonGenerate.Text = '生成密码'
$buttonGenerate.Size = New-Object System.Drawing.Size(125, 40)
$buttonGenerate.Location = New-Object System.Drawing.Point(10, 210)
$buttonGenerate.Font = New-Object System.Drawing.Font("Consolas", 12)
$buttonGenerate.Add_Click({
$selectedCount = ($checkboxes | Where-Object { $_.Checked }).Count
if ($selectedCount -lt 2) {
[System.Windows.Forms.MessageBox]::Show('请至少选择两种类型。', '错误', [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
$passwordLength = [int]$textBoxLength.Text
if ($passwordLength -le 0) {
[System.Windows.Forms.MessageBox]::Show('密码长度必须为正整数。', '错误', [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
$chars = ''
$checkboxes | Where-Object { $_.Checked } | ForEach-Object {
if ($_.Text -match '数字') { $chars += '0123456789' }
if ($_.Text -match '小写字母') { $chars += 'abcdefghijklmnopqrstuvwxyz' }
if ($_.Text -match '大写字母') { $chars += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' }
if ($_.Text -match '特殊字符') { $chars += '!@#$%&*_=[]:.?' }
}
$passwordLength = [int]$textBoxLength.Text
if ($passwordLength -le 0) {
[System.Windows.Forms.MessageBox]::Show('密码长度必须为正整数。', '错误', [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
$random = New-Object System.Random
$password = ''
for ($i = 0; $i -lt $passwordLength; $i++) {
$randomIndex = $random.Next($chars.Length)
$password += $chars[$randomIndex]
}
$textBoxPassword.Text = $password
})
$form.Controls.Add($labelLength)
$form.Controls.Add($textBoxLength)
$form.Controls.Add($textBoxPassword)
$form.Controls.Add($buttonGenerate)
[void]$form.ShowDialog()