Powershell脚本实现随机生成密码

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()
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值