如何使用 PowerShell 在 IIS 中建立和安裝網域簽署憑證?

如何使用 PowerShell 在 IIS 中建立和安裝網域簽署憑證?

在我的環境中,我們在 IIS 中託管大量網站和 WCF Web 服務(在 Windows 2008 R2 和 Windows 2012 上)。

我們正在這些網站上啟用 HTTPS。情況如下:

  • *.environment.domain使用我們網路中可用的 Active Directory 憑證授權單位在 IIS 中建立網域簽署憑證。
  • 透過變更網站綁定並套用產生的證書,在網站上啟用 HTTPS。

考慮到機器和站點的數量,我們希望自動化所需的操作。我可以在部署期間將正確的站點綁定應用到站點,但我還沒有找到在 IIS 中建立和安裝網域簽署憑證的解決方案。

我已經找到瞭如何手動執行此操作,使用 IIS 中的“伺服器證書”圖標,並在那裡調用“建立網域證書”操作。我可以提供正確的憑證,當我指定憑證授權單位時,我可以建立所需的憑證。

我還發現您可以使用 PowerShell 驅動器查看電腦中安裝的憑證cert:\

cert:\LocalMachine\My> Get-ChildItem

我發現在 Windows Server 2012 上有一個New-SelfSignedCertificatePowerShell CmdLet 可用。

但是,我似乎找不到所需的 PowerShell 命令來在 Windwos Server 2008 R2 上組合所有這些操作。

如何使用 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
    }
}

它基本上只使用 certreg.exe 而不是本機 PowerShell cmdlet(我不確定它們是否存在,如果存在,通常在較舊的作業系統上不存在)。

請求詳細資訊位於此處字串中,如果需要,請修復主題和其他設定。您可能希望將更多值移至參數部分。

我為新請求建立一個 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

相關內容