Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for logindialogwinform.ps

(view source code of logindialogwinform.ps as plain text)

  1. # PowerShell login dialog using Windows Forms.
  2. # This script is for demonstration purposes only: if you really need a
  3. # login dialog, use PowerShell's built-in Get-Credential cmdlet instead
  4. #
  5. # Form created with the PoshGUI Editor
  6. # https://poshgui.com/Editor
  7. #
  8. # Event handling edited by Rob van der Woude
  9. # https://www.robvanderwoude.com
  10.  
  11. Add-Type -AssemblyName System.Windows.Forms
  12. [System.Windows.Forms.Application]::EnableVisualStyles( )
  13.  
  14. $dialog            = New-Object System.Windows.Forms.Form
  15. $dialog.ClientSize = '300,200'
  16. $dialog.text       = "Login"
  17. $dialog.TopMost    = $true
  18.  
  19. $labelName          = New-Object System.Windows.Forms.Label
  20. $labelName.text     = "Name"
  21. $labelName.AutoSize = $true
  22. $labelName.width    = 25
  23. $labelName.height   = 10
  24. $labelName.location = New-Object System.Drawing.Point( 29, 40 )
  25. $labelName.Font     = 'Microsoft Sans Serif,10'
  26.  
  27. $textboxName           = New-Object System.Windows.Forms.TextBox
  28. # Default login name is %UserName%
  29. $textboxName.Text      = [System.Environment]::UserName
  30. $textboxName.multiline = $false
  31. $textboxName.width     = 140
  32. $textboxName.height    = 20
  33. $textboxName.location  = New-Object System.Drawing.Point( 109, 36 )
  34. $textboxName.Font      = 'Microsoft Sans Serif,10'
  35. # When Enter key is pressed while focus is on name field, move focus to password field IF and ONLY IF name field is NOT empty
  36. $textboxName.Add_KeyUp( { if ( ( $_.keyCode -eq 13 ) -and ( $textboxName.Text.Replace( ';', '' ).Trim( ) -match ".+" ) ) { $textboxPassword.focus( ) } } )
  37.  
  38. $labelPassword          = New-Object System.Windows.Forms.Label
  39. $labelPassword.text     = "Password"
  40. $labelPassword.AutoSize = $true
  41. $labelPassword.width    = 25
  42. $labelPassword.height   = 10
  43. $labelPassword.location = New-Object System.Drawing.Point( 29, 98 )
  44. $labelPassword.Font     = 'Microsoft Sans Serif,10'
  45.  
  46. $textboxPassword              = New-Object System.Windows.Forms.TextBox
  47. $textboxPassword.multiline    = $false
  48. $textboxPassword.width        = 140
  49. $textboxPassword.height       = 20
  50. $textboxPassword.location     = New-Object System.Drawing.Point( 109, 94 )
  51. $textboxPassword.Font         = 'Microsoft Sans Serif,10'
  52. # Hide the password
  53. $textboxPassword.PasswordChar = '*'
  54. # When Enter key is pressed while focus is on password field, close dialog and return result IF and ONLY IF name and password fields are NOT empty
  55. $textboxPassword.Add_KeyUp( { if ( ( $_.keyCode -eq 13 ) -and ( $textboxPassword.Text.Trim( ) -match ".+" ) -and ( $textboxName.Text.Replace( ';', '' ).Trim( ) -match ".+" ) ) { Write-Host $textboxName.Text.Replace( ';', '' ).Trim( ) -NoNewline; Write-Host ";" -NoNewline; Write-Host $textboxPassword.Text.Trim( ); $dialog.Close( ) } } )
  56.  
  57. $buttonOK          = New-Object System.Windows.Forms.Button
  58. $buttonOK.text     = "OK"
  59. $buttonOK.width    = 60
  60. $buttonOK.height   = 30
  61. $buttonOK.location = New-Object System.Drawing.Point( 109, 144 )
  62. $buttonOK.Font     = 'Microsoft Sans Serif,10'
  63. # When OK button is clicked, close dialog and return result IF and ONLY IF name and password fields are NOT empty
  64. $buttonOK.Add_Click( { if ( ( $textboxName.Text.Replace( ';', '' ).Trim( ) -match ".+" ) -and ( $textboxPassword.Text.Trim( ) -match ".+" ) ) { Write-Host $textboxName.Text.Replace( ';', '' ).Trim( ) -NoNewline; Write-Host ";" -NoNewline; Write-Host $textboxPassword.Text.Trim( ); $dialog.Close( ) } } )
  65.  
  66. $buttonCancel          = New-Object System.Windows.Forms.Button
  67. $buttonCancel.text     = "Cancel"
  68. $buttonCancel.width    = 60
  69. $buttonCancel.height   = 30
  70. $buttonCancel.location = New-Object System.Drawing.Point( 189, 144 )
  71. $buttonCancel.Font     = 'Microsoft Sans Serif,10'
  72. # When Cancel button is clicked, close the dialog without returning a result
  73. $buttonCancel.Add_Click( { $dialog.Close( ) } )
  74.  
  75. $dialog.controls.AddRange( @( $labelName, $textboxName, $labelPassword, $textboxPassword, $buttonOK, $buttonCancel ) )
  76. # Allow Esc key to cancel
  77. $dialog.CancelButton = $buttonCancel
  78.  
  79. [void] $dialog.ShowDialog( )
  80.  
  81.  

page last uploaded: 2019-01-21, 22:48