0

I'm looking for a simple script for a password-check in a Powershell GUI.

I have tried different types of codes but sometimes it can't read the textbox and sometimes it can't register the clicks.

So I have two buttons and a textbox.

  • If I have the right password and press enter I want it to do something.
  • If I have the wrong password I can try again.

But I have no idea how to do this with buttons in a GUI.

So I wonder I anyone has done something similar?

Heres what I have now about the "if" statment:

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"
        }
    }
}

1 Answer 1

2

This should do the trick:

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!"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.