Powershell密码GUI(简单)

Powershell密码GUI(简单),powershell,passwords,Powershell,Passwords,我正在寻找一个简单的脚本,用于在Powershell GUI中进行密码检查 我尝试过不同类型的代码,但有时它无法读取文本框,有时它无法注册点击 所以我有两个按钮和一个文本框 如果我有正确的密码并按enter键,我希望它能做些什么 如果我有错误的密码,我可以再试一次 但我不知道如何在GUI中使用按钮来实现这一点 所以我想知道有没有人做过类似的事情 以下是关于“如果”陈述的内容: if ($Result = [System.Windows.Forms.DialogResult]::OK) {

我正在寻找一个简单的脚本,用于在Powershell GUI中进行密码检查

我尝试过不同类型的代码,但有时它无法读取文本框,有时它无法注册点击

所以我有两个按钮和一个文本框

  • 如果我有正确的密码并按enter键,我希望它能做些什么
  • 如果我有错误的密码,我可以再试一次
但我不知道如何在GUI中使用按钮来实现这一点

所以我想知道有没有人做过类似的事情

以下是关于“如果”陈述的内容:

if ($Result = [System.Windows.Forms.DialogResult]::OK) { 
    $OKButton.Add_Click( { $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK })
    $OKButton.Add_Enter( { $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK })

    $CancelButton.Add_Click( { $CancelButton.Add_Click( { $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel }) })
    if (([string]::IsNullOrEmpty($MaskedTextBox)) -or ([string]::IsNullOrEmpty($Pword))) {
        write-host "Passwords can not be NULL or empty"
    }
    else {
        if ($MaskedTextBox -cne $PWord) {
            write-host "Fel"
        }
        else {
            ($MaskedTextBox -eq $PWord)
            Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\USBSTOR\" -Name "start" -Value 3
            write-host "Rätt"
        }
    }
}

这应该可以做到:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$password        = "Test"
$script:isMatch  = $false

[System.Windows.Forms.Application]::EnableVisualStyles()

$form = New-Object System.Windows.Forms.Form 
$form.Visible = $false
[void]$form.SuspendLayout()
$form.Text = 'Password Window'
$form.ClientSize = New-Object System.Drawing.Size(160,120) 
$form.StartPosition = 'CenterScreen'
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle

$CTRL_label1 = New-Object System.Windows.Forms.Label
$CTRL_label1.Location = New-Object System.Drawing.Point(10,10) 
$CTRL_label1.Size = New-Object System.Drawing.Size(280,20) 
$CTRL_label1.Text = 'Please enter password:'
$CTRL_label1.Name = 'Label1'
[void]$form.Controls.Add($CTRL_label1) 

$CTRL_password = New-Object System.Windows.Forms.TextBox
$CTRL_password.Location = New-Object System.Drawing.Point(10,30) 
$CTRL_password.PasswordChar = '*'
$CTRL_password.Size = New-Object System.Drawing.Size(140,20) 
$CTRL_password.MaxLength = 20
$CTRL_password.Name = 'Password'
[void]$form.Controls.Add($CTRL_password) 

$CTRL_label2 = New-Object System.Windows.Forms.Label
$CTRL_label2.Location = New-Object System.Drawing.Point(10,60) 
$CTRL_label2.Size = New-Object System.Drawing.Size(280,20) 
$CTRL_label2.ForeColor = [System.Drawing.Color]::Red
$CTRL_label2.Text = ''
$CTRL_label2.Name = 'Label2'
[void]$form.Controls.Add($CTRL_label2) 

$CTRL_Button = New-Object System.Windows.Forms.Button
$CTRL_Button.Location = New-Object System.Drawing.Point(10,90)
$CTRL_Button.Size = New-Object System.Drawing.Size(140,23)
$CTRL_Button.Text = 'OK'
$CTRL_Button.Name = 'OK'
$CTRL_Button.Add_Click( { 

    if ( $CTRL_password.Text.Trim() -ceq $password ) {
        $script:isMatch = $true
        [void]$form.Close()
        [void]$form.Dispose()
    }
    else {
        $CTRL_label2.Text = 'wrong password!'
    }


 } )
[void]$form.Controls.Add($CTRL_Button)

[void]$form.ResumeLayout()

$userInput = $form.ShowDialog()

if( $script:isMatch ) {
    # do anything => passwort is ok!
    "OK!"
}

这应该可以做到:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$password        = "Test"
$script:isMatch  = $false

[System.Windows.Forms.Application]::EnableVisualStyles()

$form = New-Object System.Windows.Forms.Form 
$form.Visible = $false
[void]$form.SuspendLayout()
$form.Text = 'Password Window'
$form.ClientSize = New-Object System.Drawing.Size(160,120) 
$form.StartPosition = 'CenterScreen'
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle

$CTRL_label1 = New-Object System.Windows.Forms.Label
$CTRL_label1.Location = New-Object System.Drawing.Point(10,10) 
$CTRL_label1.Size = New-Object System.Drawing.Size(280,20) 
$CTRL_label1.Text = 'Please enter password:'
$CTRL_label1.Name = 'Label1'
[void]$form.Controls.Add($CTRL_label1) 

$CTRL_password = New-Object System.Windows.Forms.TextBox
$CTRL_password.Location = New-Object System.Drawing.Point(10,30) 
$CTRL_password.PasswordChar = '*'
$CTRL_password.Size = New-Object System.Drawing.Size(140,20) 
$CTRL_password.MaxLength = 20
$CTRL_password.Name = 'Password'
[void]$form.Controls.Add($CTRL_password) 

$CTRL_label2 = New-Object System.Windows.Forms.Label
$CTRL_label2.Location = New-Object System.Drawing.Point(10,60) 
$CTRL_label2.Size = New-Object System.Drawing.Size(280,20) 
$CTRL_label2.ForeColor = [System.Drawing.Color]::Red
$CTRL_label2.Text = ''
$CTRL_label2.Name = 'Label2'
[void]$form.Controls.Add($CTRL_label2) 

$CTRL_Button = New-Object System.Windows.Forms.Button
$CTRL_Button.Location = New-Object System.Drawing.Point(10,90)
$CTRL_Button.Size = New-Object System.Drawing.Size(140,23)
$CTRL_Button.Text = 'OK'
$CTRL_Button.Name = 'OK'
$CTRL_Button.Add_Click( { 

    if ( $CTRL_password.Text.Trim() -ceq $password ) {
        $script:isMatch = $true
        [void]$form.Close()
        [void]$form.Dispose()
    }
    else {
        $CTRL_label2.Text = 'wrong password!'
    }


 } )
[void]$form.Controls.Add($CTRL_Button)

[void]$form.ResumeLayout()

$userInput = $form.ShowDialog()

if( $script:isMatch ) {
    # do anything => passwort is ok!
    "OK!"
}