PowerShell을 사용하여 IIS에서 도메인 서명 인증서를 만들고 설치하려면 어떻게 해야 하나요?

PowerShell을 사용하여 IIS에서 도메인 서명 인증서를 만들고 설치하려면 어떻게 해야 하나요?

내 환경에서는 IIS(Windows 2008 R2 및 Windows 2012)에서 수많은 웹 사이트와 WCF 웹 서비스를 호스팅합니다.

우리는 이러한 사이트에서 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 cmdlet 대신 certreg.exe를 사용합니다(존재하는지 확실하지 않으며, 존재하더라도 일반적으로 이전 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

관련 정보