天天看點

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

繼續閱讀