問題
数百台サーバの証明書をいちいち接続して更新してたら飽きてきた。メンバーと分けてやろうとしたが指示自体が恥ずかしくなり・・・
毎年やることなんでなんとかしましょう
- 環境
- Active Directory + Windows Server 2012 + Windows Server 2012 R2 + Windows Server 2016 サーバーはどんどん増えている
- zabbix
1.まずはどのくらい期限が迫っているか確認
- zabbix
- 証明書期限監視
- アイテム作成はこちら参照: https://qiita.com/mkawanee/items/c392c1609f459962ec12
- 有効期限確認はzabbixの「監視データ」→「最新データ」でグループ指定してリストで眺める。便利だー
2.グループポリシーでファイルとタスクを配布
- Active Directory
- コンピューターの構成 → 基本設定 → Windowsの設定 → ファイル
C:\share\ファイル配信\ps1\Update-Cert.ps1
C:\share\ファイル配信\pfx\awscloud.com.pfx
- コンピューターの構成→基本設定→コントロールパネルの設定→タスク
タスク名: update-cert
ユーザアカウント:NT AUTHORITY\System
ユーザがログオンしているかどうかにかかわらず実行する:オン
操作:PowerShell.exe -command C:\share\ps1\Update-Cert.ps1 -CPW {証明書パスワード}
トリガー:一回限り、繰り返し間隔:1時間、継続時間:1日
繰り返し間隔:チェック外す
3.次日にzabbixで確認
4.片付け
- 証明書が各サーバーに配布されているのと、タスクに証明書パスワードが書かれているので両方のアクションを「削除」に変更
Update-Cert.ps1
- DSCがよいけど。
param (
[string]$CPW,
[string]$ADName = "ad-server"
)
Import-Module RemoteDesktop
Import-Module WebAdministration
function Update-Cert
{
param (
[string]$CPW,
[string]$ADName
)
$ExpireInDays = Get-ChildItem -Path Cert:\LocalMachine\My |
Select-Object -Property Subject, @{n='ExpireInDays';e={($_.notafter - (Get-Date)).Days}} |
Where-Object {$_.Subject -like "*awscloud.com*"} | Select-Object -Property ExpireInDays
$days = $ExpireInDays.ExpireInDays
$sys = Get-WmiObject Win32_ComputerSystem
if ($days -lt 60) {
$serverFullname = $sys.Name + "." + $sys.Domain
$certPath = "C:\share\ps1\awscloud.pfx"
if (Test-Path $certPath) {
$password = ConvertTo-SecureString -String "${CPW}" -AsPlainText -Force
# Update Cert for IIS
$Cert = Get-ChildItem -Path Cert:\LocalMachine\My | where-Object {$_.subject -like "*awscloud.com*"} | Select-Object -ExpandProperty Thumbprint
certutil -delstore My $Cert
certutil -p $CPW -importpfx $certPath
$Cert = Get-ChildItem -Path Cert:\LocalMachine\My | where-Object {$_.subject -like "*awscloud.com*"} | Select-Object -ExpandProperty Thumbprint
Get-Item -Path Cert:\localmachine\my\$Cert | Set-Item -path IIS:\SslBindings\0.0.0.0!443
# Update Cert for RemoteApp
Set-RDCertificate -Role RDWebAccess -ImportPath $certPath -Password $password -ConnectionBroker $serverFullname -Force
Set-RDCertificate -Role RDRedirector -ImportPath $certPath -Password $password -ConnectionBroker $serverFullname -Force
Set-RDCertificate -Role RDPublishing -ImportPath $certPath -Password $password -ConnectionBroker $serverFullname -Force
}
}
#Write-Output ("`r`n" + $sys.Name + "," + $ExpireInDays.ExpireInDays + "`r`n---")
Write-Output $days
}
Update-Cert -CPW $CPW -ADName $ADName
メモ
約300台分、問題なく更新できたと。