Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
在IIS Server 的應用程式集區(application pool) 中,我們常常會使用網域使用者作為應用程式集區的服務帳號。
當網域policy有規定密碼有效日期後,應用程式集區的服務帳號密碼就需要定期進行修改,若忘記修改導致密碼過期,應用程式集區可能會無法啟動(因為預設1740分鐘,應用程式集區會進行回收),導致佈署在該集區的應用程式會無法執行。
當有數百個服務帳號密碼需要修改時,我們可以使用PowerShell將此作業自動化。
圖一, IIS 應用程式集區
PowerShell Sample
下列範例分為兩段功能,首先先修改AD的網域使用者密碼,接下來再修改 IIS 應用程式集區的識別密碼(identity password)。
而修改 IIS 應用程式集區的識別密碼,又再細分為三個步驟: 停止集區 -> 修改密碼 -> 啟動集區。
Import-Module ActiveDirectory
Import-Module WebAdministration
# Set the New password
$newPWD = ConvertTo-SecureString -AsPlainText $newPassword -Force
# Reset user password in AD
Set-ADAccountPassword -Identity $samAccountName -NewPassword $newPWD -Reset
$userName = "yourDomainName\$samAccountName"
# Change AppPool identity password in IIS
$targetpool = Get-item "IIS:\AppPools\$appPoolName"
$targetpool.Stop()
$targetpool | Set-ItemProperty -Name "processModel.identitytype" -Value 3
$targetpool | Set-ItemProperty -Name "processModel.username" -Value $userName
$targetpool | Set-ItemProperty -Name "processModel.password" -Value $newPassword
$targetpool.Start()
Enjoy!