oɔɯ*_*ɯǝɹ 5 powershell iis-7 iis-7.5 windows-server-2008-r2 ad-certificate-services
在我的环境中,我们在 IIS(在 Windows 2008 R2 和 Windows 2012 上)中托管了大量网站和 WCF Web 服务。
我们正在这些网站上启用 HTTPS。这如下:
*.environment.domain使用我们网络中可用的 Active Directory 证书颁发机构,在 IIS 中创建域签名证书。鉴于机器和站点的数量,我们希望自动化所需的操作。我可以在部署期间将正确的站点绑定应用到站点,但我还没有找到在 IIS 中创建和安装域签名证书的解决方案。
我已经找到了如何手动执行此操作,使用 IIS 中的“服务器证书”图标,并在那里调用“创建域证书”操作。我可以提供正确的凭据,并且当我指定证书颁发机构时,我可以创建所需的证书。
我还发现您可以使用cert:\PowerShell 驱动器查看机器中安装的证书:
cert:\LocalMachine\My> Get-ChildItem
Run Code Online (Sandbox Code Playgroud)
我发现在 Windows Server 2012 上有一个New-SelfSignedCertificatePowerShell CmdLet 可用。
但是,我似乎找不到在 Windwos Server 2008 R2 上组合所有这些操作所需的 PowerShell 突击队员。
如何使用 PowerShell 在 IIS 中创建和安装域签名的 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
}
}
Run Code Online (Sandbox Code Playgroud)
它基本上只使用 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3416 次 |
| 最近记录: |