Share via

Need help with powershell DO WHILE statement...

BTor 81 Reputation points
2021-06-18T07:39:59.18+00:00

Hi,
Sorry but I'm a powershell newbie and I would be glad if someone could help me with this. I have created a test dialog where users can enter their password twice. When they click the OK button the script shall compare $PasswordBox.Text with $PasswordBox2.Text and if they are not equal then the dialog should not be closed (or should reopen) and focus set to object $PasswordBox.

How do I do that? I really don't know the behaviour and possibilities for forms and dialog.

I hope you understand my problem. Thanks a lot for any help.

best regards Tor

Script:

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

function frGetNewUserCreds {

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Create local user....'
$form.Size = New-Object System.Drawing.Size(300,300)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,230)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,230)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$PasswordBox = New-Object System.Windows.Forms.MaskedTextBox
$PasswordBox.Location = New-Object System.Drawing.Point(10,140)
$PasswordBox.PasswordChar = '*'
$PasswordBox.Size = New-Object System.Drawing.Size(150,20)
$form.Controls.Add($PasswordBox)

$PasswordBox2 = New-Object System.Windows.Forms.MaskedTextBox
$PasswordBox2.Location = New-Object System.Drawing.Point(10,190)
$PasswordBox2.PasswordChar = '*'
$PasswordBox2.Size = New-Object System.Drawing.Size(150,20)
$form.Controls.Add($PasswordBox2)

$PasswordBox.Text = "password1"
$PasswordBox2.Text = "password2"

$form.Topmost = $true

$form.Add_Shown({$PasswordBox.Select()})

$result = $form.ShowDialog()

if (($Result -eq "OK") -AND (-NOT ($PasswordBox.Text -ceq $PasswordBox2.Text))) {

Here is the challenge :-)
Do not close dialog BUT keep dialog open and set focus to $PasswordBox

write-host "`nHere the dialog should not be closed!"
}

return $Result, $FullNameBox.Text, $UsernameBox.Text, $PasswordBox.Text, $PasswordBox2.Text
}

$Result, $FullName, $username, $password, $password2 = frGetNewUserCreds

write-host "`nReturned parameters: $Result $FullName $username $password $password2"

Windows for business | Windows Server | User experience | PowerShell

3 answers

Sort by: Most helpful
  1. Anonymous
    2021-06-19T13:16:37+00:00

    Hi,

    I tried $this.DialogResult = [System.Windows.Forms.DialogResult]::OK and it didn't work either. However, you may try this workaround.

    $okButton.Add_MouseDown({  
        if (($PasswordBox.Text -ceq $PasswordBox2.Text) -and ($PasswordBox.Text)) {  
            $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK  
        }  
    })  
    $okButton.add_click({  
        if (-not ($PasswordBox.Text -ceq $PasswordBox2.Text) -OR ($PasswordBox.Text -eq "")) {  
            if ($PasswordBox.Text -eq "") {  
                [System.Windows.Forms.MessageBox]::Show("Passwords cannot be empty")  
            }  
            else{  
                [System.Windows.Forms.MessageBox]::Show("Passwords didn’t match. Please try again.")  
            }  
        }  
    })  
    $result = $form.ShowDialog()  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

  2. BTor 81 Reputation points
    2021-06-18T13:46:24.413+00:00

    Hi again,

    Thanks a lot!!

    This does the trick, there is only a minor issue - I need to click the OK button twice when the two passwords match. Am I missing something - or is this the price to pay?

    In any case I'm very grateful for the tip as I now can test this properly :-)

    best regards Tor

    I paste in my code below:

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

    function frGetNewUserCreds {

    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Create local user....'
    $form.Size = New-Object System.Drawing.Size(300,300)
    $form.StartPosition = 'CenterScreen'

    $okButton = New-Object System.Windows.Forms.Button
    $okButton.Location = New-Object System.Drawing.Point(75,230)
    $okButton.Size = New-Object System.Drawing.Size(75,23)
    $okButton.Text = 'OK'

    $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK

    $form.AcceptButton = $okButton
    $form.Controls.Add($okButton)

    $cancelButton = New-Object System.Windows.Forms.Button
    $cancelButton.Location = New-Object System.Drawing.Point(150,230)
    $cancelButton.Size = New-Object System.Drawing.Size(75,23)
    $cancelButton.Text = 'Cancel'
    $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $form.CancelButton = $cancelButton
    $form.Controls.Add($cancelButton)

    $PasswordBox = New-Object System.Windows.Forms.MaskedTextBox
    $PasswordBox.Location = New-Object System.Drawing.Point(10,140)

    $PasswordBox.PasswordChar = '*'

    $PasswordBox.Size = New-Object System.Drawing.Size(150,20)
    $form.Controls.Add($PasswordBox)

    $PasswordBox2 = New-Object System.Windows.Forms.MaskedTextBox
    $PasswordBox2.Location = New-Object System.Drawing.Point(10,190)

    $PasswordBox2.PasswordChar = '*'

    $PasswordBox2.Size = New-Object System.Drawing.Size(150,20)
    $form.Controls.Add($PasswordBox2)

    $PasswordBox.Text = "password1"

    $PasswordBox2.Text = "password2"

    $form.Topmost = $true

    $form.Add_Shown({$PasswordBox.Select()})

    $okButton.add_click({
    if ((-not ($PasswordBox.Text -ceq $PasswordBox2.Text)) -OR ($PasswordBox.Text -eq "")) {
    if ($PasswordBox.Text -eq "") {
    [System.Windows.Forms.MessageBox]::Show("Passwords cannot be empty")
    } else {
    [System.Windows.Forms.MessageBox]::Show("Passwords didn’t match. Please try again.")
    }
    }
    else
    {
    $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    }
    })

    $result = $form.ShowDialog()

    return $Result, $FullNameBox.Text, $UsernameBox.Text, $PasswordBox.Text, $PasswordBox2.Text
    }

    $Result, $FullName, $username, $password, $password2 = frGetNewUserCreds

    write-host "`nReturned parameters: $Result $FullName $username $password $password2"

    Was this answer helpful?

    0 comments No comments

  3. Anonymous
    2021-06-18T08:46:55.49+00:00

    Hi,

    You can remove the line $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK and replace the if block with this

    $okButton.add_click({  
        if (-not ($PasswordBox.Text -ceq $PasswordBox2.Text)) {  
            [System.Windows.Forms.MessageBox]::Show("Passwords didn’t match. Please try again.")   
        }  
        else{  
            $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK  
        }  
    })  
    $result = $form.ShowDialog()  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.