Windows Server 2008 R2中的Active Directory Users新增時會預設42天後密碼自動失效,需要管理者重新reset密碼方能重新使用。在此分享幾個PowerShell指令獲取想要的資訊與利用cmd大量修改user屬性。

Powershell

### 取的所有密碼過期的users ```

get-aduser -filter {Enabled -eq $True} -properties passwordExpired | where {$_.passwordExpired} | Select DistinguishedName

<h3><!--more-->取得所有"password never expires"屬性為false的users,並把結果輸出到C:\temp.txt此檔案(這是我需要的)。</h3>
註:此輸出的temp.txt檔為UCS-2編碼,若需要其他程式用途需自行轉成UTF-8或其他想要的格式(如:Notepad++)。

get-aduser -filter {PasswordNeverExpires -eq $False -AND Enabled -eq $True} | Select Distinguishedname > C:\temp.txt

### 取得所有啟用的users詳細資訊(欄位有:Name, PasswordLastSet, PasswordExpired, PasswordAge)。

Get-ADUser -filter {Enabled -eq $True} -properties * | Sort PasswordLastSet | Select Name,PasswordLastSet,PasswordExpired,@{Name="PasswordAge";Expression={(Get-Date)-$_.PasswordLastSet}}


Reference from: <a href="http://www.scriptlogic.com/smbit/article/track-user-password-expiration-using-active-directory">http://www.scriptlogic.com/smbit/article/track-user-password-expiration-using-active-directory</a>
<h2><span style="color: #3366ff;">CMD</span></h2>
dsmod是cmd中的Directory Service Command-line tools,其他詳細資訊可用dsmod /? 查詢。在此分享的為用dsmod重設使用者密碼、並將pwdneverexpired屬性設為yes。

dsmod user "CN=abc,CN=Users,DC=microsoft,DC=com" -pwd abc@123 -mustchpwd no -pwdneverexpires yes


其中user的部分為PowerShell中輸出的DistinguishedName。
<h2><span style="color: #3366ff;">Ruby Script</span></h2>
有了上述兩部分的指令與結果後,就用ruby簡單寫了個產生batch檔的程式。記得使用前要把temp.txt上方的輸出欄位刪除。

w = File.open("resetpwd.bat", "w+")
File.open("temp.txt", "r") do |f|
while (line = f.gets)
w.write("dsmod user &quot;#{line.to_s.strip}&quot; -pwd abc@123 -mustchpwd no -pwdneverexpires yes\n")
end
end
w.close



<img style="position: absolute !important; z-index: -1 !important; right: 1px !important; top: -20px !important; cursor: pointer !important; -webkit-border-radius: 20px; background-color: rgba(200, 200, 200, 0.3) !important; padding: 3px 5px 0 !important; margin: 0 !important;" onclick="document.location.href='http://translate.google.com/';" src="http://www.google.com/uds/css/small-logo.png" alt="" />