在我的环境中,我们在IIS(Windows 2008 R2和Windows 2012)中托pipe了大量网站和WCF Web服务。
我们正在这些网站上启用HTTPS。 这个过程如下:
*.environment.domain证书。 鉴于机器和网站的数量,我们希望自动执行所需的操作。 我可以在部署过程中将正确的站点绑定应用于站点,但是我还没有find在IIS中创build和安装域签名证书的解决scheme。
我已经find了如何手动执行此操作,使用IIS中的“服务器证书”图标,并在那里调用“创build域证书”操作。 我可以提供正确的凭据,并且当我指定证书颁发机构时,我可以创build所需的证书。
我还发现,您可以查看使用cert:\ PowerShell驱动器安装在机器上的cert:\ :
cert:\LocalMachine\My> Get-ChildItem
我发现在Windows Server 2012上有一个New-SelfSignedCertificate PowerShell CmdLet可用。
不过,我似乎无法find所需的PowerShell突击队在Windwos Server 2008 R2上结合所有这些操作。
如何使用PowerShell在IIS中创build和安装域签名的SSL证书?
尝试以下操作:
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,(我不确定它们是否存在,如果是这样,通常不会在较旧的操作系统上)。
请求详细信息在这里string中,如果需要,修复主题和其他设置。 您可能想要将更多的值移动到参数部分。
我为新请求创build一个inf文件并将其转换为请求文件。
然后,我将请求提交给$ CAName中指定的CA,如果执行用户是域pipe理员,则请求立即发出。
最后我用-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