Author Posts

August 17, 2016 at 3:17 pm

I've created the below form that will unlock/reset passwords.

The 'Reset' button prompts a message box to appear that the user will put in the new password for. If it does not meet complexity requirements it will prompt the user for a new password, but say if they click the wrong user and want to cancel, it wont allow it.

I'm not really sure what to change to allow for the cancel button to work...

Any help would be greatly appreciated

function test-complexity($pass)
{

	$stringx=$pass

	$regex = "[^a-zA-Z0-9]" #check for special characters
	$validity = ""
	If ($stringx –cmatch $regex) {
	  $validity= "true"
	  $msg = "Special Character YES`n"   
	}
	Else
	{
	 $validity = "false"
	  $msg +=  "Please include Special Character/s in your password`n"  
	}


	$regex1 = "[a-z]"
	If ($stringx –cmatch $regex1) {
	  $validity= $validity + "true"
	  $msg +=  "Lower Case YES`n"
	}
	Else
	{
	$validity= $validity +  "false"
	  $msg +=  "Please include lower Character/s in your password`n"
	}


	$regex2 = "[A-Z]"
	If ($stringx –cmatch $regex2) {
	  $validity=  $validity + "true"
	  $msg +=  "Upper Case YES`n"
	}
	Else
	{
	$validity= $validity + "false"
	  $msg +=  "Please include upper Character/s in your password`n"
	}


	$regex3 = "[0-9]"
	If ($stringx –cmatch $regex3) {
	   $validity= $validity + "true"
	  $msg +=  "Number in String YES`n"
	}
	Else
	{
	$validity= $validity + "false"
	  $msg +=  "Please include number/s in your password`n"
	}


	$count = $stringx.Length

	if ($count -le 11) {
	 $validity= $validity + "false"
	$msg +=  "Password should at least be 12 characters or more`n"
	}
	else
	{
	$msg +=  "At least 12 characters YES`n"
	$validity= $validity +  "true"
	}

	#just to display the number of characters
	#write-host "$stringx has $count Characters"

	#check if validity contains false
	$check_validity = $validity.Contains("false")
	 

	if ($check_validity -eq "True")
	{
		$msg +=  "Password does not meet complexity, FAIL`n"
		
		[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null
		$oReturn=[System.Windows.Forms.Messagebox]::Show($MSG)
		return "BAD"
	}
}



function Call-lockedout_psf {

	#----------------------------------------------
	#region Import the Assemblies
	#----------------------------------------------
	[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	[void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
	#endregion Import Assemblies

	#----------------------------------------------
	#region Generated Form Objects
	#----------------------------------------------
	[System.Windows.Forms.Application]::EnableVisualStyles()
	$formAccountUnlock = New-Object 'System.Windows.Forms.Form'
	$Reset = New-Object 'System.Windows.Forms.Button'
	$Close = New-Object 'System.Windows.Forms.Button'
	$buttonRefresh = New-Object 'System.Windows.Forms.Button'
	$Unlock = New-Object 'System.Windows.Forms.Button'
	$LockedList = New-Object 'System.Windows.Forms.CheckedListBox'
	$timer1 = New-Object 'System.Windows.Forms.Timer'
	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
	#endregion Generated Form Objects

	#----------------------------------------------
	# User Generated Script
	#----------------------------------------------
	
	$formAccountUnlock_Load={
		#TODO: Initialize Form Controls here
		$lockedlist.CheckOnClick = $true
		
		Load-ListBox $LockedList (Search-ADAccount -LockedOut) "name"
	
		# timer1
		#
		$timer1.Enabled = $True
		$timer1.Interval = 60000
		$timer1.add_Tick($timer1_Tick)
	}
	
	#region Control Helper Functions
	function Load-ListBox 
	{
	
		Param (
			[ValidateNotNull()]
			[Parameter(Mandatory=$true)]
			[System.Windows.Forms.ListBox]$ListBox,
			[ValidateNotNull()]
			[Parameter(Mandatory=$true)]
			$Items,
		    [Parameter(Mandatory=$false)]
			[string]$DisplayMember,
			[switch]$Append
		)
		
		if(-not $Append)
		{
			$listBox.Items.Clear()	
		}
		
		if($Items -is [System.Windows.Forms.ListBox+ObjectCollection] -or $Items -is [System.Collections.ICollection])
		{
			$listBox.Items.AddRange($Items)
		}
		elseif ($Items -is [System.Collections.IEnumerable])
		{
			$listBox.BeginUpdate()
			foreach($obj in $Items)
			{
				$listBox.Items.Add($obj)
			}
			$listBox.EndUpdate()
		}
		else
		{
			$listBox.Items.Add($Items)	
		}
	
		$listBox.DisplayMember = $DisplayMember	
	}
	#endregion
	
	$Unlock_Click = {
		#TODO: Place custom script here
		$checked = $LockedList.CheckedItems
		
		foreach ($item in $checked)
		{
			Unlock-ADAccount $item
		}
		
		$LockedList.Items.clear()
		Load-ListBox $LockedList (Search-ADAccount -LockedOut) "name"
		
	}
	
	
	$timer1_Tick={
		#TODO: Place custom script here
		$LockedList.Items.clear()
		Load-ListBox $LockedList (Search-ADAccount -LockedOut) "name"
	}
	$buttonRefresh_Click={
		#TODO: Place custom script here
		$LockedList.Items.clear()
		Load-ListBox $LockedList (Search-ADAccount -LockedOut) "name"
	}
	
	$Reset_Click={
		#TODO: Place custom script here
		
		
		$checked = $LockedList.CheckedItems
		
		foreach ($item in $checked)
		{
			do
			{
				[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
				$Pass = [Microsoft.VisualBasic.Interaction]::InputBox("Enter a new password for $($item.name)", "Passsword Rest")
				
				$passCheck = test-complexity $pass
			}
			until($passCheck -ne "BAD")
			
			#$secpass = ConvertTo-SecureString -String $pass -AsPlainText -Force
			#Set-ADAccountPassword -Identity $item -reset -NewPassword $secpass
		}
	}
	
	$Close_Click={
	
		$formAccountUnlock.Close()
	}
	
	# --End User Generated Script--
	#----------------------------------------------
	#region Generated Events
	#----------------------------------------------
	
	$Form_StateCorrection_Load=
	{
		#Correct the initial state of the form to prevent the .Net maximized form issue
		$formAccountUnlock.WindowState = $InitialFormWindowState
	}
	
	$Form_Cleanup_FormClosed=
	{
		#Remove all event handlers from the controls
		try
		{
			$Reset.remove_Click($Reset_Click)
			$Close.remove_Click($Close_Click)
			$buttonRefresh.remove_Click($buttonRefresh_Click)
			$Unlock.remove_Click($Unlock_Click)
			$formAccountUnlock.remove_Load($formAccountUnlock_Load)
			$timer1.remove_Tick($timer1_Tick)
			$formAccountUnlock.remove_Load($Form_StateCorrection_Load)
			$formAccountUnlock.remove_FormClosed($Form_Cleanup_FormClosed)
		}
		catch [Exception]
		{ }
	}
	#endregion Generated Events

	#----------------------------------------------
	#region Generated Form Code
	#----------------------------------------------
	$formAccountUnlock.SuspendLayout()
	#
	# formAccountUnlock
	#
	$formAccountUnlock.Controls.Add($Reset)
	$formAccountUnlock.Controls.Add($Close)
	$formAccountUnlock.Controls.Add($buttonRefresh)
	$formAccountUnlock.Controls.Add($Unlock)
	$formAccountUnlock.Controls.Add($LockedList)
	$formAccountUnlock.AutoScaleDimensions = '6, 13'
	$formAccountUnlock.AutoScaleMode = 'Font'
	$formAccountUnlock.ClientSize = '369, 184'
	$formAccountUnlock.Cursor = 'Default'
	$formAccountUnlock.Name = 'formAccountUnlock'
	$formAccountUnlock.Text = 'Account Unlock'
	$formAccountUnlock.add_Load($formAccountUnlock_Load)
	#
	# Reset
	#
	$reset.Location = '274, 44'
	$reset.Name = 'Reset'
	$reset.Size = '75, 23'
	$reset.TabIndex = 1
	$reset.Text = 'Reset'
	$reset.UseVisualStyleBackColor = $True
	$reset.add_Click($reset_Click)
	#
	# Close
	#
	$Close.Location = '274,103'
	$Close.name = "Close"
	$Close.size = '75,23'
	$Close.TabIndex = 3
	$Close.Text = "Close"
	$Close.UseVisualStyleBackColor = $true
	$Close.add_Click($Close_Click)
	
	#
	# buttonRefresh
	#
	$buttonRefresh.Location = '274, 73'
	$buttonRefresh.Name = 'buttonRefresh'
	$buttonRefresh.Size = '75, 23'
	$buttonRefresh.TabIndex = 2
	$buttonRefresh.Text = 'Refresh'
	$buttonRefresh.UseVisualStyleBackColor = $True
	$buttonRefresh.add_Click($buttonRefresh_Click)
	#
	# Unlock
	#
	$Unlock.Location = '274, 15'
	$Unlock.Name = 'Unlock'
	$Unlock.Size = '75, 23'
	$Unlock.TabIndex = 0
	$Unlock.Text = 'Unlock'
	$Unlock.UseVisualStyleBackColor = $True
	$Unlock.add_Click($Unlock_Click)
	#
	# LockedList
	#
	$LockedList.FormattingEnabled = $True
	$LockedList.Location = '12, 15'
	$LockedList.Name = 'LockedList'
	$LockedList.Size = '256, 154'
	$LockedList.TabIndex = 4
	#
	# timer1
	#
	$timer1.add_Tick($timer1_Tick)
	$formAccountUnlock.ResumeLayout()
	#endregion Generated Form Code

	#----------------------------------------------

	#Save the initial state of the form
	$InitialFormWindowState = $formAccountUnlock.WindowState
	#Init the OnLoad event to correct the initial state of the form
	$formAccountUnlock.add_Load($Form_StateCorrection_Load)
	#Clean up the control events
	$formAccountUnlock.add_FormClosed($Form_Cleanup_FormClosed)
	#Show the Form
	return $formAccountUnlock.ShowDialog()

} #End Function

#Call the form
Call-lockedout_psf | Out-Null
  • This topic was modified 2 years, 1 month ago by  ertuu85.

August 17, 2016 at 4:32 pm

You won't be able to, you're form is stuck in the do until loop so you can't press the cancel button. Remove the do until, use the errorprovider control, validate the password on the reset click, if not valid show the error provider.

August 17, 2016 at 4:33 pm

btw, sapien has scripting support forums. Post the psf there and I'll fix it for you if you're stuck.

August 17, 2016 at 8:07 pm

Thanks Dan,

Got pointed in the right direction and got it working using:

$Reset_Click={
		#TODO: Place custom script here
		
		
		$checked = $LockedList.CheckedItems
		
		foreach ($item in $checked)
		{
			[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
			[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
			$objForm = New-Object System.Windows.Forms.Form 
			$objForm.Text = "Password Reset"
			$objForm.Size = New-Object System.Drawing.Size(300,200) 
			$objForm.StartPosition = "CenterScreen"
			$objForm.KeyPreview = $True
			$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
				{$x=$objTextBox.Text;$objForm.Close()}})
			$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
				{$objForm.Close()}})
			# Add Ok Button
			$OKButton = New-Object System.Windows.Forms.Button
			$OKButton.Location = New-Object System.Drawing.Size(75,120)
			$OKButton.Size = New-Object System.Drawing.Size(75,23)
			$OKButton.Text = "OK"

			$OKButton.Add_Click({
				
			$passtest = test-complexity $objTextBox.Text
			
				If($passtest -eq $null) # Valid
				{
					$objForm.Close()
				}
				Else # Invalid
				{
					
					$ErrorProvider.SetError($objTextbox, $passtest);
				}

			})

			$objForm.Controls.Add($OKButton)
			# Add Cancel Button
			$CancelButton = New-Object System.Windows.Forms.Button
			$CancelButton.Location = New-Object System.Drawing.Size(150,120)
			$CancelButton.Size = New-Object System.Drawing.Size(75,23)
			$CancelButton.Text = "Cancel"
			$CancelButton.Add_Click({$objForm.Close()})
			$objForm.Controls.Add($CancelButton)
			# Add Textbox Label
			$objLabel = New-Object System.Windows.Forms.Label
			$objLabel.Location = New-Object System.Drawing.Size(10,20) 
			$objLabel.Size = New-Object System.Drawing.Size(280,20) 
			#$objLabel.Size = New-Object System.Drawing.Size(280,20) 
			$objLabel.Text = "Enter a new Password for $($item.name):"
			$objForm.Controls.Add($objLabel) 
			# Add Textbox
			$objTextBox = New-Object System.Windows.Forms.TextBox 
			$objTextBox.Location = New-Object System.Drawing.Size(10,40) 
			$objTextBox.Size = New-Object System.Drawing.Size(140,80) 
			$objForm.Controls.Add($objTextBox) 
			# Add Validation Control
			$ErrorProvider = New-Object System.Windows.Forms.ErrorProvider
			   
			$objForm.Topmost = $True
			$objForm.Add_Shown({$objForm.Activate()})
			[void] $objForm.ShowDialog()
		
			$secpass = ConvertTo-SecureString -String $objTextbox.text -AsPlainText -Force
			Set-ADAccountPassword -Identity $item -reset -NewPassword $secpass
		}
	}