need help with powershell, close on exit and validation

lupinlicious 136 Reputation points
2022-02-13T16:25:26.66+00:00

I need to some help with closing the form after when you click "Create Account" and when it says "User has been created successfully" otherwise MDT won't continue

I also need some help with make a confirm password so they match each other.

function Load-Form {

    $Form.Controls.Add($GBAccountProperties)
    $Form.Controls.Add($TBUserName)
    $Form.Controls.Add($GBUserName)
    $Form.Controls.Add($TBPassword)
    $Form.Controls.Add($GBPassword)
    $Form.Controls.Add($Button)
    $Form.Controls.Add($Label)
    $Form.Add_Shown({$Form.Activate()})
    [void] $Form.ShowDialog()
}

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$Global:ErrorProvider = New-Object System.Windows.Forms.ErrorProvider

$Form = New-Object System.Windows.Forms.Form    
$Form.Size = New-Object System.Drawing.Size(540, 240) 
$Form.StartPosition = "CenterScreen"
$Form.SizeGripStyle = "Hide"
$Form.Text = "Enter Local Account"
$Form.ControlBox = $false
$Form.TopMost = $true

# TextBox - UserName

$TBUserName = New-Object System.Windows.Forms.TextBox
$TBUserName.Location = New-Object System.Drawing.Size(25,30) 
$TBUserName.Size = New-Object System.Drawing.Size(215,50)
$TBUserName.TabIndex = "1"

# TextBox - Password

$TBPassword = New-Object System.Windows.Forms.TextBox
$TBPassword.Location = New-Object System.Drawing.Size(25,110) 
$TBPassword.Size = New-Object System.Drawing.Size(215,50)
$TBPassword.TabIndex = "2"

# GroupBox - Username

$GBUserName = New-Object System.Windows.Forms.GroupBox
$GBUserName.Location = New-Object System.Drawing.Size(20,10) 
$GBUserName.Size = New-Object System.Drawing.Size(225,50) 
$GBUserName.Font = [System.Drawing.Font]::new("Corbel", 9, [System.Drawing.FontStyle]::Regular)
$GBUserName.Text = "User Name:" 

# GroupBox - Password

$GBPassword = New-Object System.Windows.Forms.GroupBox
$GBPassword.Location = New-Object System.Drawing.Size(20,90)  
$GBPassword.Size = New-Object System.Drawing.Size(225,50) 
$GBPassword.Font = [System.Drawing.Font]::new("Corbel", 9, [System.Drawing.FontStyle]::Regular)
$GBPassword.Text = "Password:" 

# GroupBox - Account Properties

$GBAccountProperties = New-Object System.Windows.Forms.GroupBox
$GBAccountProperties.Location = New-Object System.Drawing.Size(253,10)
$GBAccountProperties.Size = New-Object System.Drawing.Size(250,150)
$GBAccountProperties.Name = 'groupbox3'
$GBAccountProperties.TabIndex = 3
$GBAccountProperties.TabStop = $False
$GBAccountProperties.Text = 'Account Properties'

# CheckBox1 - Account Never Expires

$checkBox1 = New-Object System.Windows.Forms.CheckBox
$checkBox1.Location = New-Object System.Drawing.Size(262,27)
$checkBox1.Size = New-Object System.Drawing.Size(185, 24)
$checkBox1.Name = 'checkBox1'
$checkBox1.TabIndex = 0
$checkBox1.Text = 'Account Never Expires'
$checkBox1.UseVisualStyleBackColor = $True
$form.Controls.Add($checkBox1)

# CheckBox2 - AdminAccount

$checkBox2          = New-Object System.Windows.Forms.CheckBox
$checkBox2.Location = New-Object System.Drawing.Size (262,54)
$checkBox2.Size     = New-Object System.Drawing.Size(185,24)
$checkBox2.Text     = "AdminAccount"
$checkBox2.Name = 'checkBox2'
$checkBox2.TabIndex = 0
$checkBox2.UseVisualStyleBackColor = $True
$form.Controls.Add($checkBox2)

# Checkbox3 - Password Never Expires

$checkBox3 = New-Object System.Windows.Forms.CheckBox
$checkBox3.Location = New-Object System.Drawing.Size(262, 79)
$checkBox3.Size = New-Object System.Drawing.Size(185, 24)
$checkBox3.Name = 'checkBox3'
$checkBox3.TabIndex = 0
$checkBox3.UseVisualStyleBackColor = $True
$checkBox3.Text = 'Password Never Expires'
$form.Controls.Add($checkBox3)


# Checkbox4 - You Can Change the Password

$checkBox4 = New-Object System.Windows.Forms.CheckBox
$checkBox4.Location = New-Object System.Drawing.Size(262, 104)
$checkBox4.Size = New-Object System.Drawing.Size(185, 24)
$checkBox4.Name = 'checkBox4'
$checkBox4.TabIndex = 0
$checkBox4.UseVisualStyleBackColor = $True
$checkBox4.Text = 'You Can Change the Password'
$checkBox4.UseVisualStyleBackColor = $True
$form.Controls.Add($checkBox4)

# Checkbox5 - No Password

$checkBox5 = New-Object System.Windows.Forms.CheckBox
$checkBox5.Location = New-Object System.Drawing.Size(262, 129)
$checkBox5.Size = New-Object System.Drawing.Size(185, 24)
$checkBox5.Name = 'checkBox5'
$checkBox5.TabIndex = 0
$checkBox5.Text = 'no Password'
$checkBox5.UseVisualStyleBackColor = $True
$form.Controls.Add($checkBox5)

$checkbox5.Add_Click({

        # Disable/Enable Other Controls Depending On State Of Current Checkbox
        $checkBox1.Enabled = !$checkBox5.Checked
        $TBPassword.Enabled = !$checkBox5.Checked
        $checkbox2.Enabled = !$checkBox5.Checked
})
    $Button = New-Object System.Windows.Forms.Button
    $Button.Location = New-Object System.Drawing.Size(13, 165)
    $Button.Size = New-Object System.Drawing.Size(492, 28)
    $Button.Name = 'Button'
    $Button.TabIndex = 4
    $Button.Text = 'Create Account'
    $Button.UseVisualStyleBackColor = $True

    $Button.Add_Click({

    # Admin or Users Group
    $group = @{$true='S-1-5-32-544';$false='S-1-5-32-545'}[$checkbox2.checked]
    try{

        # Define Options To Create User
        $useroptions = @{
            Name = $TBUserName.Text
            Description = $TBUserName.Text
            Fullname = $TBUserName.Text
            AccountNeverExpires = $checkbox1.Checked
            UserMayNotChangePassword = !$checkbox4.Checked
        }

        # If The "noPassword" Checkbox Is Not Checked
        if (!$checkbox5.Checked){
            $useroptions.Password = ConvertTo-SecureString $TBPassword.Text -AsPlainText -Force
            $useroptions.PasswordNeverExpires = $checkbox1.Checked
        }else{

        # "noPassword" Checkbox Is Checked
            $useroptions.NoPassword = $true
            $group = 'S-1-5-32-545'
        }

        # Create User And Assign To Administrators Group
        New-LocalUser @useroptions | Add-LocalGroupMember -Group (Get-Localgroup | ? Sid -eq $group)
        [System.Windows.Forms.MessageBox]::Show("User has been created successfully.","User created",0,64) 
    }catch{
        [System.Windows.Forms.MessageBox]::Show("Error creating new user account:`n $($_.Exception.Message)","Exception",0,48)
        return
    }


})


Load-Form

Thank you so much!

Microsoft Deployment Toolkit
Microsoft Deployment Toolkit
A collection of Microsoft tools and documentation for automating desktop and server deployment. Previously known as Microsoft Solution Accelerator for Business Desktop Deployment (BDD).
888 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,514 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 33,381 Reputation points
    2022-02-14T15:30:22.39+00:00

    Your special character test isn't correct.

      # Reference: https://ss64.com/ps/syntax-regex.html
    
      $TBPassword = 'ax2$xMxx'        # change this to test
    
      ($TBPassword -cmatch '[a-z]')   
      ($TBPassword -cmatch '[A-Z]') 
      ($TBPassword -match '\d') 
      ($TBPassword.length -gt 7) 
      ($TBPassword -match '[!@#%^&$]')
    
    
    
    if (($TBPassword -cmatch '[a-z]') -and ($TBPassword -cmatch '[A-Z]') -and ($TBPassword -match '\d') -and ($TBPassword.length -gt 7) -and ($TBPassword -match '[!@#%^&$]')) {
        "All tests are true."
    } else { 
        [System.Windows.Forms.MessageBox]::Show("Passwords must be at least 7 characters, contain an uppercase and a special character.") 
        return
    }
    

2 additional answers

Sort by: Most helpful
  1. lupinlicious 136 Reputation points
    2022-02-13T19:00:01.087+00:00

    OK, I think I got it now, would you do something else with the form?

    function Load-Form {

        $Form.Controls.Add($GBAccountProperties)
        $Form.Controls.Add($TBUserName)
        $Form.Controls.Add($GBUserName)
        $Form.Controls.Add($TBPassword)
        $Form.Controls.Add($TBPassword2)
        $Form.Controls.Add($GBPassword)
        $Form.Controls.Add($Button)
        $Form.Controls.Add($Label)
        $Form.Add_Shown({$Form.Activate()})
        [void] $Form.ShowDialog()
    }
    
    $Button.add_click({
         if (-not ($TBPassword.Text -ceq $TBPassword2.Text)) {
             [System.Windows.Forms.MessageBox]::Show("Passwords didn’t match. Please try again.") 
         }
         else{
             $Button.DialogResult = [System.Windows.Forms.DialogResult]::OK
         }
     })
    
    
    
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
    
    $Global:ErrorProvider = New-Object System.Windows.Forms.ErrorProvider
    
    $Form = New-Object System.Windows.Forms.Form    
    $Form.Size = New-Object System.Drawing.Size(540, 240) 
    $Form.StartPosition = "CenterScreen"
    $Form.SizeGripStyle = "Hide"
    $Form.Text = "Enter Local Account"
    $Form.ControlBox = $false
    $Form.TopMost = $true
    
    # TextBox - UserName
    
    $TBUserName = New-Object System.Windows.Forms.TextBox
    $TBUserName.Location = New-Object System.Drawing.Size(25,30) 
    $TBUserName.Size = New-Object System.Drawing.Size(215,50)
    $TBUserName.TabIndex = "1"
    
    # TextBox - Password
    
    $TBPassword = New-Object System.Windows.Forms.TextBox
    $TBPassword.Location = New-Object System.Drawing.Size(25,90) 
    $TBPassword.Size = New-Object System.Drawing.Size(215,50)
    $TBPassword.TabIndex = "2"
    $TBPassword.PasswordChar = '*'
    
    $TBPassword2 = New-Object System.Windows.Forms.TextBox
    $TBPassword2.Location = New-Object System.Drawing.Size(25, 120) 
    $TBPassword2.Size = New-Object System.Drawing.Size(215,50)
    $TBPassword2.TabIndex = "2"
    $TBPassword2.PasswordChar = '*'
    
    
    # GroupBox - Username
    
    $GBUserName = New-Object System.Windows.Forms.GroupBox
    $GBUserName.Location = New-Object System.Drawing.Size(20,10) 
    $GBUserName.Size = New-Object System.Drawing.Size(225,50) 
    $GBUserName.Font = [System.Drawing.Font]::new("Corbel", 9, [System.Drawing.FontStyle]::Regular)
    $GBUserName.Text = "User Name:" 
    
    # GroupBox - Password
    
    $GBPassword = New-Object System.Windows.Forms.GroupBox
    $GBPassword.Location = New-Object System.Drawing.Size(20,70)  
    $GBPassword.Size = New-Object System.Drawing.Size(225,80) 
    $GBPassword.Font = [System.Drawing.Font]::new("Corbel", 9, [System.Drawing.FontStyle]::Regular)
    $GBPassword.Text = "Password:" 
    
    
    # GroupBox - Account Properties
    
    $GBAccountProperties = New-Object System.Windows.Forms.GroupBox
    $GBAccountProperties.Location = New-Object System.Drawing.Size(253,10)
    $GBAccountProperties.Size = New-Object System.Drawing.Size(250,150)
    $GBAccountProperties.Name = 'groupbox3'
    $GBAccountProperties.TabIndex = 3
    $GBAccountProperties.TabStop = $False
    $GBAccountProperties.Text = 'Account Properties'
    
    # CheckBox1 - Account Never Expires
    
    $checkBox1 = New-Object System.Windows.Forms.CheckBox
    $checkBox1.Location = New-Object System.Drawing.Size(262,27)
    $checkBox1.Size = New-Object System.Drawing.Size(185, 24)
    $checkBox1.Name = 'checkBox1'
    $checkBox1.TabIndex = 0
    $checkBox1.Text = 'Account Never Expires'
    $checkBox1.UseVisualStyleBackColor = $True
    $form.Controls.Add($checkBox1)
    
    # CheckBox2 - AdminAccount
    
    $checkBox2          = New-Object System.Windows.Forms.CheckBox
    $checkBox2.Location = New-Object System.Drawing.Size (262,54)
    $checkBox2.Size     = New-Object System.Drawing.Size(185,24)
    $checkBox2.Text     = "AdminAccount"
    $checkBox2.Name = 'checkBox2'
    $checkBox2.TabIndex = 0
    $checkBox2.UseVisualStyleBackColor = $True
    $form.Controls.Add($checkBox2)
    
    # Checkbox3 - Password Never Expires
    
    $checkBox3 = New-Object System.Windows.Forms.CheckBox
    $checkBox3.Location = New-Object System.Drawing.Size(262, 79)
    $checkBox3.Size = New-Object System.Drawing.Size(185, 24)
    $checkBox3.Name = 'checkBox3'
    $checkBox3.TabIndex = 0
    $checkBox3.UseVisualStyleBackColor = $True
    $checkBox3.Text = 'Password Never Expires'
    $form.Controls.Add($checkBox3)
    
    
    # Checkbox4 - You Can Change the Password
    
    $checkBox4 = New-Object System.Windows.Forms.CheckBox
    $checkBox4.Location = New-Object System.Drawing.Size(262, 104)
    $checkBox4.Size = New-Object System.Drawing.Size(185, 24)
    $checkBox4.Name = 'checkBox4'
    $checkBox4.TabIndex = 0
    $checkBox4.UseVisualStyleBackColor = $True
    $checkBox4.Text = 'You Can Change the Password'
    $checkBox4.UseVisualStyleBackColor = $True
    $form.Controls.Add($checkBox4)
    
    # Checkbox5 - No Password
    
    $checkBox5 = New-Object System.Windows.Forms.CheckBox
    $checkBox5.Location = New-Object System.Drawing.Size(262, 129)
    $checkBox5.Size = New-Object System.Drawing.Size(185, 24)
    $checkBox5.Name = 'checkBox5'
    $checkBox5.TabIndex = 0
    $checkBox5.Text = 'no Password'
    $checkBox5.UseVisualStyleBackColor = $True
    $form.Controls.Add($checkBox5)
    
    $checkbox5.Add_Click({
    
            # Disable/Enable Other Controls Depending On State Of Current Checkbox
            $checkBox1.Enabled = !$checkBox5.Checked
            $TBPassword.Enabled = !$checkBox5.Checked
            $checkbox2.Enabled = !$checkBox5.Checked
    })
        $Button = New-Object System.Windows.Forms.Button
     $Button.Location = New-Object System.Drawing.Size(13, 165)
     $Button.Size = New-Object System.Drawing.Size(492, 28)
     $Button.Name = 'Button'
     $Button.TabIndex = 4
     $Button.Text = 'Create Account'
     $Button.UseVisualStyleBackColor = $True
    
        $Button.Add_Click({
    
        # Admin or Users Group
        $group = @{$true='S-1-5-32-544';$false='S-1-5-32-545'}[$checkbox2.checked]
        try{
    
            # Define Options To Create User
            $useroptions = @{
                Name = $TBUserName.Text
                Description = $TBUserName.Text
                Fullname = $TBUserName.Text
                AccountNeverExpires = $checkbox1.Checked
                UserMayNotChangePassword = !$checkbox4.Checked
            }
    
            # If The "noPassword" Checkbox Is Not Checked
            if (!$checkbox5.Checked){
                $useroptions.Password = ConvertTo-SecureString $TBPassword.Text -AsPlainText -Force
                $useroptions.PasswordNeverExpires = $checkbox1.Checked
            }
            else
            {
    
            # "noPassword" Checkbox Is Checked
                $useroptions.NoPassword = $true
                $group = 'S-1-5-32-545'
            }
    
               # Check So The Password Match 
               if (-not ($TBPassword.Text -ceq $TBPassword2.Text)) {
             [System.Windows.Forms.MessageBox]::Show("Passwords didn’t match. Please try again.") 
                return
            }
    
            # Create User And Assign To Administrators Group
            New-LocalUser @useroptions | Add-LocalGroupMember -Group (Get-Localgroup | ? Sid -eq $group)
            [System.Windows.Forms.MessageBox]::Show("User has been created successfully.","User created",0,64) 
            $Form.Close()
        }catch{
            [System.Windows.Forms.MessageBox]::Show("Error creating new user account:`n $($_.Exception.Message)","Exception",0,48)
            return
        }
    
    
    })
    
    Load-Form
    
    0 comments No comments

  2. lupinlicious 136 Reputation points
    2022-02-14T07:59:14.32+00:00

    I'm struggling with the password validation, I'd like to add atleast 8 characters, special characters and one uppercase.
    The result it that I can create passwords less than 4 characters

            if(($TBPassword -cmatch '[a-z]') -and ($TBPassword -cmatch '[A-Z]') -and ($TBPassword -match '\d') -and ($TBPassword.length -gt 7) -and ($TBPassword -match '!|@|#|%|^|&|$')) {
                [System.Windows.Forms.MessageBox]::Show("Passwords must atelast be 7 charactersgain.") 
                    return
                    }
    
    
               # Check So The Password Match  
               if (-not ($TBPassword.Text -ceq $TBPassword2.Text))   {
             [System.Windows.Forms.MessageBox]::Show("Passwords didn’t match. Please try again.") 
                return
            }
    
            # Create User And Assign To Administrators Group
            New-LocalUser @useroptions | Add-LocalGroupMember -Group (Get-Localgroup | ? Sid -eq $group)
            [System.Windows.Forms.MessageBox]::Show("User has been created successfully.","User created",0,64) 
            $Form.Close()
        }catch{
            [System.Windows.Forms.MessageBox]::Show("Error creating new user account:`n $($_.Exception.Message)","Exception",0,48)
            return
        }
    

    Best regards

    0 comments No comments

Your answer

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