简体   繁体   中英

Powershell Ad Reset Utility

Hoping to get some help on this AD Password Reset utility. I'm building a form using .Net objects but when I execute the script it runs the set-adaccountpassword cmdlet before I have a chance to select variable values for password and Username from dropdown. I cant seem to get the execution order of the code correct. Please Help!

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

$Form = New-Object System.Windows.Forms.Form    
$Form.Size = New-Object System.Drawing.Size(600,275)
$form.Name="UCEDC Password Reset"  
$form.BackColor="lightGreen"
$form.Text="Company Password Reset Utility"

############################################## Start functions

#$computer=$DropDownBox.SelectedItem.ToString() #populate the var with the value you selected

############################################## end functions

############################################## Start drop down boxes

$DropDownBox = New-Object System.Windows.Forms.ComboBox
$DropDownBox.Location = New-Object System.Drawing.Size(20,50) 
$DropDownBox.Size = New-Object System.Drawing.Size(180,20) 
$DropDownBox.DropDownHeight = 200 
$Form.Controls.Add($DropDownBox) 

$wksList=@(get-aduser -filter *| select-object -ExpandProperty samaccountname)

foreach ($wks in $wksList) {
                      $DropDownBox.Items.Add($wks)
                              } #end foreach
$passworchange=$DropDownBox.SelectedItem

############################################## end drop down boxes

$Button = New-Object System.Windows.Forms.Button 
$Button.Location = New-Object System.Drawing.Size(400,30) 
$Button.Size = New-Object System.Drawing.Size(110,80) 
$Button.Text = "Reset Password"

$button.add_click({[Microsoft.VisualBasic.interaction]::inputbox("Enter a Password","Password",$passworchange) })

##$Button.Add_Click({procInfo})

$Form.Controls.Add($Button) 

Set-ADAccountPassword -Identity $username -NewPassword $passworchange

############################################## Start text fields

$outputBox = New-Object System.Windows.Forms.TextBox 
$outputBox.Location = New-Object System.Drawing.Size(10,150) 
$outputBox.Size = New-Object System.Drawing.Size(565,200) 
$outputBox.MultiLine = $True 
$outputBox.ScrollBars = "Vertical" 
$outputBox.text= $passworchange
$Form.Controls.Add($outputBox) 

############################################## end text fields

############################################## Start buttons

############################################## end buttons

$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()

The issue is that you need to put your Set-ADAccountPassword line within the ScriptBlock for your button click, but after the prompt for the password.

$button.add_click({$PasswordChange = [Microsoft.VisualBasic.interaction]::inputbox("Enter a Password","Password","Change Me"); Set-ADAccountPassword -Identity $username -NewPassword $passwordchange})

Probably a better idea is to generate a random password, set the user's password to that, and then inform them of what that password is. Something that I have used in the past is:

#Load the [System.Web] assembly so we can generate passwords later on
[System.Reflection.Assembly]::LoadWithPartialName("System.Web")|out-null
#Generate a 15 character complex password, with between 3 and 10 non-alpha characters
[string]$Password = [System.Web.Security.Membership]::GeneratePassword(15,(get-random -min 3 -max 10))
$Password|CLIP

That sets $Password to a randomly generated password and copies that password to the clipboard (in case you need to email to somebody or paste it in a spreadsheet or whatever).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM