LoginSignup
2
2

More than 3 years have passed since last update.

大量サーバー群(Windows)の証明書を一括更新

Last updated at Posted at 2019-05-08

問題

数百台サーバの証明書をいちいち接続して更新してたら飽きてきた。メンバーと分けてやろうとしたが指示自体が恥ずかしくなり・・・

毎年やることなんでなんとかしましょう

  • 環境
  • Active Directory + Windows Server 2012 + Windows Server 2012 R2 + Windows Server 2016 サーバーはどんどん増えている
  • zabbix

1.まずはどのくらい期限が迫っているか確認

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台分、問題なく更新できたと。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2