Reputation: 1
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.
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"
}
}
}
Upvotes: 0
Views: 2761
Reputation: 1782
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!"
}
Upvotes: 2