PowerShell を使用して IIS にドメイン署名証明書を作成してインストールするにはどうすればよいですか?

PowerShell を使用して IIS にドメイン署名証明書を作成してインストールするにはどうすればよいですか?

私の環境では、IIS (Windows 2008 R2 および Windows 2012) で多数の Web サイトと WCF Web サービスをホストしています。

これらのサイトで HTTPS を有効にするプロセスが進行中です。手順は次のとおりです。

  • *.environment.domainネットワークで利用可能な Active Directory 証明機関を使用して、IIS でドメイン署名証明書を作成します。
  • サイト バインディングを変更し、生成された証明書を適用して、サイトで HTTPS を有効にします。

マシンとサイトの数を考えると、必要なアクションを自動化したいと考えています。展開中にサイトに正しいサイト バインディングを適用できますが、IIS でドメイン署名証明書を作成してインストールするためのソリューションはまだ見つかっていません。

私はすでに、IIS の「サーバー証明書」アイコンを使用して、そこから「ドメイン証明書の作成」アクションを呼び出すことで、これを手動で行う方法を見つけました。正しい資格情報を提供し、証明機関を指定すると、必要な証明書を作成できます。

また、PowerShell ドライブを使用して、マシンにインストールされている証明書を表示できることもわかりましたcert:\

cert:\LocalMachine\My> Get-ChildItem

また、Windows Server 2012 では PowerShell CmdLet が利用できることがわかりましたNew-SelfSignedCertificate

しかし、Windwos Server 2008 R2 でこれらすべてのアクションを組み合わせるために必要な PowerShell コマンドが見つからないようです。

PowerShell を使用して IIS でドメイン署名付き SSL 証明書を作成し、インストールするにはどうすればよいですか?

答え1

次のことを試してください。

function New-DomainSignedCertificate {
    [CmdletBinding()]
    param(
        [parameter(Mandatory=$true)]
        [string]
        $Hostname,

        [parameter(Mandatory=$true)]
        [string]
        $Organization,

        [parameter(Mandatory=$true)]
        [string]
        $OrganizationalUnit,

        [parameter(Mandatory=$true)]
        [string]
        $Locality,

        [parameter(Mandatory=$true)]
        [string]
        $State,

        [parameter(Mandatory=$true)]
        [string]
        $Country,

        [parameter(Mandatory=$true)]
        [string]
        $CertificateAuthority,

        [parameter(Mandatory=$false)]
        [string]
        $Keylength = "2048",

        [string]
        $workdir = $env:Temp
    )

    $fileBaseName = $Hostname -replace "\.", "_" 
    $fileBaseName = $fileBaseName -replace "\*", ""

    $infFile = $workdir + "\" + $fileBaseName + ".inf"
    $requestFile = $workdir + "\" + $fileBaseName + ".req"
    $CertFileOut = $workdir + "\" + $fileBaseName + ".cer"

    Try {
        Write-Verbose "Creating the certificate request information file ..."
        $inf = @"
[Version] 
Signature="`$Windows NT`$"

[NewRequest]
Subject = "CN=$Hostname, OU=$OrganizationalUnit, O=$Organization, L=$Locality, S=$State, C=$Country"
KeySpec = 1
KeyLength = $Keylength
Exportable = TRUE
FriendlyName = "$Hostname"
MachineKeySet = TRUE
SMIME = False
PrivateKeyArchive = FALSE
UserProtected = FALSE
UseExistingKeySet = FALSE
ProviderName = "Microsoft RSA SChannel Cryptographic Provider"
ProviderType = 12
RequestType = PKCS10
KeyUsage = 0xa0

[Extensions]
2.5.29.17 = "{text}"
_continue_ = "dns=$Hostname&"
"@

        $inf | Set-Content -Path $infFile

        Write-Verbose "Creating the certificate request ..."
        & certreq.exe -new "$infFile" "$requestFile"

        Write-Verbose "Submitting the certificate request to the certificate authority ..."
        & certreq.exe -submit -config "$CertificateAuthority" -attrib "CertificateTemplate:WebServer" "$requestFile" "$CertFileOut"

        if (Test-Path "$CertFileOut") {
            Write-Verbose "Installing the generated certificate ..."
            & certreq.exe -accept "$CertFileOut"
        }

    }
    Finally {
        Get-ChildItem "$workdir\$fileBaseName.*" | remove-item
    }
}

基本的には、ネイティブの PowerShell コマンドレットではなく certreg.exe を使用します (ネイティブの PowerShell コマンドレットが存在するかどうかはわかりませんが、存在する場合でも、古い OS には通常存在しません)。

リクエストの詳細はここの文字列にあります。必要に応じて件名やその他の設定を修正してください。さらに多くの値をパラメータ セクションに移動することもできます。

新しいリクエスト用の inf ファイルを作成し、それをリクエスト ファイルに変換します。

次に、$CAName で指定された CA にリクエストを送信します。実行ユーザーがドメイン管理者の場合、リクエストはすぐに発行されます。

最後に、-accept を使用してリクエストを完了し、クリーンアップを実行します。

通常、新しい証明書も PFX ファイルにエクスポートします。

IIS バインディングと証明書の割り当てには、次のようなものを使用できます。

 New-WebBinding -Name www.test.local   -Port 443 -Protocol https
 $thumb = (Get-ChildItem cert:\LocalMachine\MY | where-object { $_.FriendlyName -like "www.test.local" } | Select-Object -First 1).Thumbprint
 $guid = [guid]::NewGuid()
 & netsh http add sslcert hostnameport=www.test.local:443 certhash=$thumb appid=`{$guid`} certstorename=MY

関連情報