powershellwinformstextboxpasswordsinputbox

Powershell InputBox (selfmade) only returns "Cancel"


`In a Powershell project - small GUI with Winforms - I need a passwordbox. I wrote a test-function but cannot manage to get the correct output ... I only get "Cancel" I get the same result if I do not use

$inpBox.PasswordChar      = "*"
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[System.Windows.Forms.Application]::EnableVisualStyles()

function InputForm {

    param($loggedInUser="Spock")
  
    $inpForm                  = New-Object System.Windows.Forms.Form
    $inpForm.Height           = 120
    $inpForm.Width            = 350
    # $inpForm.Icon             = "$PSScriptRoot\Heidelberg-H.ico"
    $inpForm.Text             = "Authentication Window"
    $inpForm.MaximizeBox      = $false
    $inpForm.MinimizeBox      = $false
    $inpForm.FormBorderStyle  = "FixedDialog"
    $inpForm.StartPosition    = "CenterScreen"  # moves to center of screen
    $inpForm.Topmost          = $true           # Make it Topmost
  
    # create a Label
    $inpLbl                   = New-Object System.Windows.Forms.Label
    $inpLbl.Width             = 300
    $inpLbl.Location          = New-Object System.Drawing.Point(10,10)
    $inpLbl.Text              = "Please type in the password for user: " + $loggedInUser
  
    # create a InputBox for the password
    $inpBox                   = New-Object System.Windows.Forms.TextBox
    $inpBox.Width             = 200
    $inpBox.Height            = 25
    $inpBox.PasswordChar      = "*"
    $inpBox.Text              = "********"
    $inpBox.Location          = New-Object System.Drawing.Point(10, 35)     
    
    # create an OK Button
    $inpBtn                   = New-Object System.Windows.Forms.Button
    $inpBtn.Width             = 60
    $inpBtn.Height            = 25
    $inpBtn.Text              = "OK"
    $inpBtn.Location          = New-Object System.Drawing.Point(250,33)
    $form.AcceptButton        = $inpBtn
  
    $inpForm.Controls.AddRange(@($inpLbl, $inpBox, $inpBtn))
  
    # OK-Button - Click event
    $inpBtn.Add_Click({

        $inpForm.Close()
        return $inpBox.Text

    })  
   
    $inpForm.ShowDialog()
}

# MAIN

$PW = InputForm (Get-WMIObject -ClassName Win32_ComputerSystem).Username

write-host $PW

Solution

  • The problem is with this last part of your function:

    $inpBtn.Add_Click({
    
        $inpForm.Close()
        return $inpBox.Text
    
    })  
    
    $inpForm.ShowDialog()
    

    Instead of registering a custom handler for the Click event, you'll want to assign a value to the DialogResult property on the button - then inspect that result and return the value of the textbox from the function:

    $inpBtn.DialogResult = 'OK' 
    
    if($inpForm.ShowDialog() -eq 'OK'){
        return $inpBox.Text
    }
    else {
        throw "User canceled password prompt!"
    }