'powershell gui creating local users

i need some help with my powershell gui. i want to create a gui for creating local useraccounts with some parameters provided in the checkbox i need to click. the gui is running but i need some help for the code lines for the checkboxes. what do i need to add that the code is running fone when i click the checkboxes? for example if i just click checkbox 1 i get an error the username is to long even i name the testaccount "testuser"

$PSDefaultParameterValues['*:Encoding'] = 'ascii'
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing    

#create form
$form             = New-Object System.Windows.Forms.Form
$form.Width       = 500
$form.Height      = 400
$form.MaximizeBox = $false
$form.TopMost     = $true

$objLabel = New-Object System.Windows.Forms.label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(130,15)
$objLabel.BackColor = "Transparent"
$objLabel.ForeColor = "Black"
$objLabel.Text = "name"
$Form.Controls.Add($objLabel)

#textbox with choosen user name
$txtBox          = New-Object System.Windows.Forms.TextBox
$txtBox.Location = New-Object System.Drawing.Point (180, 20)
$txtBox.Size     = New-Object System.Drawing.Size(280,100)
$form.Controls.Add($txtBox)

$objLabel2 = New-Object System.Windows.Forms.label
$objLabel2.Location = New-Object System.Drawing.Size(10,50)
$objLabel2.Size = New-Object System.Drawing.Size(130,15)
$objLabel2.BackColor = "Transparent"
$objLabel2.ForeColor = "Black"
$objLabel2.Text = "password"
$Form.Controls.Add($objLabel2)

#textbox with choosen password 
$txtBox2          = New-Object System.Windows.Forms.TextBox
$txtBox2.Location = New-Object System.Drawing.Point (180, 50)
$txtBox2.Size     = New-Object System.Drawing.Size(280,100)
$form.Controls.Add($txtBox2)

#create checkbox1
$checkBox          = New-Object System.Windows.Forms.CheckBox
$checkBox.Location = New-Object System.Drawing.Point (10, 100)
$checkBox.Size     = New-Object System.Drawing.Size(350,30)
$checkBox.Text     = "PasswordNeverExpires"
$form.Controls.Add($checkBox)

#create checkbox2
$checkBox2          = New-Object System.Windows.Forms.CheckBox
$checkBox2.Location = New-Object System.Drawing.Point (10, 150)
$checkBox2.Size     = New-Object System.Drawing.Size(350,30)
$checkBox2.Text     = "UserMayChangePassword"
$form.Controls.Add($checkBox2)

#create checkbox2
$checkBox3          = New-Object System.Windows.Forms.CheckBox
$checkBox3.Location = New-Object System.Drawing.Point (10, 200)
$checkBox3.Size     = New-Object System.Drawing.Size(350,30)
$checkBox3.Text     = "AccountNeverExpires"
$form.Controls.Add($checkBox3)

#create user button
$Button          = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(10,250)
$Button.Size     = New-Object System.Drawing.Size(150,50)
$Button.Text     = "create user"
$Button.Add_Click({
if(($checkBox.Checked -eq $false) -and ($checkBox2.Checked -eq $false) -and ($checkBox3.Checked -eq $false)) {
[System.Windows.Forms.Messagebox]::Show("No CheckBox checked")
}    
             
#checkbox1 action
if ($checkBox.Checked -eq $true) {
                  $adminName          = $txtBox
                  $securePassword     = $txtBox2
                 $newUser = New-LocalUser -Name $adminName -Password $securePassword -Description $adminName -FullName $adminName #-ErrorAction Stop
                 $newUser | Set-LocalUser <#-PasswordNeverExpires $true#> -UserMayChangePassword $false <#-AccountNeverExpires#> #-ErrorAction Stop
                 Add-LocalGroupMember -Group "Administrators" -Member $adminName #-ErrorAction Stop




    if(-not $?) {[System.Windows.Forms.MessageBox]::Show( "no success",'','OK',"Error")}
else {[System.Windows.Forms.MessageBox]::Show( "success",'','OK',"Information")} 
                             }

 #checkbox2 action
 if ($checkBox2.Checked -eq $true) {
    if(-not $?) {[System.Windows.Forms.MessageBox]::Show( "no success",'','OK',"Error")}
else {[System.Windows.Forms.MessageBox]::Show( "success",'','OK',"Information")}                                   
                            }  

 #checkbox3 action
 if ($checkBox3.Checked -eq $true) {
    if(-not $?) {[System.Windows.Forms.MessageBox]::Show( "no success",'','OK',"Error")}
else {[System.Windows.Forms.MessageBox]::Show( "success",'','OK',"Information")}                                   
                            }  


  })
  $form.Controls.Add($Button2)


 #end
 [void]$form.ShowDialog()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source