天天看点

AD 重置密码完整脚本

上篇文档我们介绍了如何创建一个UI输入框,下面我们将介绍一下怎么讲输入框中的内容作为一个变量传递到一个脚本里面:

<a href="http://lixiaosong.blog.51cto.com/attachment/201312/8/705126_1386507179xXFc.png"></a>

#为了保证完整性我们把上述的代码内容再复制一遍

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

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

$objForm = New-Object System.Windows.Forms.Form    

$objForm.Text = "Reset Password"    

$objForm.Size = New-Object System.Drawing.Size(300,140)     

$objForm.StartPosition = "CenterScreen"    

$objForm.KeyPreview = $True

$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")    

   {$username=$objTextBox.Text;$objForm.Close()}})    

$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")     

   {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button   

$OKButton.Location = New-Object System.Drawing.Size(55,65)    

$OKButton.Size = New-Object System.Drawing.Size(75,23)    

$OKButton.Text = "OK"    

$OKButton.Add_Click({$username=$objTextBox.Text;$objForm.Close()})    

$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button   

$CancelButton.Location = New-Object System.Drawing.Size(170,65)    

$CancelButton.Size = New-Object System.Drawing.Size(75,23)    

$CancelButton.Text = "Cancel"    

$CancelButton.Add_Click({$objForm.Close()})    

$objForm.Controls.Add($CancelButton)

$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.Text = "Please enter UserName:"    

$objForm.Controls.Add($objLabel)

$objTextBox = New-Object System.Windows.Forms.TextBox    

$objTextBox.Location = New-Object System.Drawing.Size(10,40)     

$objTextBox.Size = New-Object System.Drawing.Size(260,20)     

$objForm.Controls.Add($objTextBox)

$objForm.Add_Shown({$objForm.Activate()})   

[void] $objForm.ShowDialog()

#首先我们创建一个随机密码的函数:

function password    

{    

#定义组合元素,分别是数字、小写和大写字母    

$number=12,34,56,78,90;    

$lowercase="ab","de","fg","hi","jk","mp","qr","tu","vr","wy";    

$blockcase="AB","DE","FG","HJ","KL","MN","QR","TU","VW","XY";

$order=New-Object System.Collections.ArrayList;   

for($i=0;$i -lt 10;$i++)    

[void]$order.Add($i);    

}    

$neworder=@();    

$produceOrder=Get-Random -InputObject $order;    

$neworder+=$produceOrder;    

$order.Remove($produceOrder);    

$newpassword=@();    

foreach ($i in $neworder)    

if ($i -eq 0)    

$index=Get-Random -Maximum 5;    

$newpassword+=$number[$index];    

if ($i -eq 1)    

$index=Get-Random -Maximum 10;    

$newpassword+=$lowercase[$index];    

if ($i -eq 2)    

$newpassword+=$blockcase[$index];    

if ($i -eq 3)    

if ($i -eq 4)    

return $newPassword -join "";    

}

$pass=password #将上述的函数转换为一个变量

$pwd=ConvertTo-SecureString "$pass" -AsPlainText   -Force

get-aduser $username | Set-ADAccountPassword -Reset -newpassword $pwd #查询上述输入框传递过来的变量内容(用户),通过管道给这个用户重置一个新密码。

get-aduser $username | Set-ADUser -ChangePasswordAtLogon $true #将这个用户设置为下次登录必须更改密码

echo “$username 密码重置为$pass”     #显示重置后的结果

好了一个通过UI输入框完成AD账户密码重置的脚本就完成了。我们测试一下:

输入一个AD账户:

<a href="http://lixiaosong.blog.51cto.com/attachment/201312/8/705126_1386507179ZPeD.png"></a>

账户重置后的结果显示

<a href="http://lixiaosong.blog.51cto.com/attachment/201312/8/705126_1386507180Gg51.png"></a>

本文转自handsome7038 51CTO博客,原文链接:http://blog.51cto.com/lixiaosong/1338148

下一篇: Dubbo+Zookeeper

继续阅读