我集中管理域控制器,但是站点管理员在本地管理自己的数字发送器。我可以通过向导轻松地将X509证书(不需要私钥)与整个链从Windows Server 2008 R2域控制器导出到p7b文件:
~~~~~~~~~~~~~~~~~
... 5。将打开“证书导出向导”。单击下一步。
在“导出文件格式”对话框中,执行以下操作:
a。选择“加密消息语法标准– PKCS#7证书(.P7B)”。
b。如果可能,请选中在证书路径中包括所有证书。
c。单击下一步。
在要导出的文件对话框中,单击浏览。
在“另存为”对话框中,执行以下操作:
a。在“文件名”框中,键入ciroots.p7b。
b。在“保存类型”框中,选择“ PKCS#7证书(* .p7b)”。
c。单击保存。
在要导出的文件对话框中,单击下一步。
在“完成证书导出向导”页面上,单击“完成”。
~~~~~~~~~~~~~~~~~
效果很好。生成的文件可以很好地导入数字发送器进行身份验证。如果站点管理员还没有导入其他证书,它可以使站点管理员访问它们。它不需要包含私钥,因为没有私钥,它可以正常工作。
问题是,我需要为每个业务站点手动执行数十次此操作,因为每个业务站点都有自己的域控制器,每个域控制器都有自己的证书。必须有一种方法可以自动执行此证书导出(带有/.NET的PowerShell,certutil.exe等)。也许某些东西使用System.Security.Cryptography.X509Certificate X509IncludeOption和WholeChain,但是我无法使其正常工作:
$ Cert =(dir Cert:\ localmachine \ my)[0]
具有#p7b文件扩展名的## PKCS7证书导出。
$ CertCollection =新对象
System.Security.Cryptography.X509Certificates.X509Certificate2Collection
$ Cert | %{[void] $ CertCollection.Add($ _)}
$ Exported_pkcs7 = $ CertCollection.Export('Pkcs7')
$ out_FileName = $ ENV:COMPUTERNAME +“ .p7b”
$ My_Export_Path ='d:\ CertFiles \'+ $ out_FileName
设置内容-路径$ My_Export_Path-值$ Exported_pkcs7-编码字节
使用此代码,我仅获得证书,而不获得其链中的其余证书。我不需要整个脚本,只需要复制可以通过GUI手动完成的导出w / chain的部分。
function Export-Certificate {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[Security.Cryptography.X509Certificates.X509Certificate2]$Certificate,
[Parameter(Mandatory = $true)]
[IO.FileInfo]$OutputFile,
[switch]$IncludeAllCerts
)
$certs = New-Object Security.Cryptography.X509Certificates.X509Certificate2Collection
if ($IncludeAllCerts) {
$chain = New-Object Security.Cryptography.X509Certificates.X509Chain
$chain.ChainPolicy.RevocationMode = "NoCheck"
[void]$chain.Build($Certificate)
$chain.ChainElements | ForEach-Object {[void]$certs.Add($_.Certificate)}
$chain.Reset()
} else {
[void]$certs.Add($Certificate)
}
Set-Content -Path $OutputFile.FullName -Value $certs.Export("pkcs7") -Encoding Byte
}
function Export-CertificateChain {
<#
.SYNOPSIS
Create p7b certificate container.
.DESCRIPTION
Create p7b certificate container.
.EXAMPLE
PS C:\> ls "C:\PKI Trust Chain\Certificates" -File -Recurse | Get-PfxCertificate | where issuer -match 'Internal|External' | Export-CertificateChain -OutputFile C:\Temp\PKITrustChain.p7b
Loop thru the folder "C:\PKI Trust Chain\Certificates" (assuming it contains only certificates), load the certficiates and add all certificates where issuer matches into the p7b file.
.EXAMPLE
PS C:\> ls "cert:\localMachine\" -Recurse | where issuer -match 'Internal|External' | Export-CertificateChain -OutputFile C:\Temp\PKITrustChain.p7b
Loop thru the certificate stroe where issuer matches and adds them into the p7b file.
.INPUTS
[Security.Cryptography.X509Certificates.X509Certificate2], [IO.FileInfo]
.OUTPUTS
None.
.NOTES
Author: Patrick Sczepanski (Vadims Podans)
Original script found: https://stackoverflow.com/questions/33512409/automate-export-x509-certificate-w-chain-from-server-2008-r2-to-a-p7b-file-witho
Version: 20200505
#>
[CmdletBinding()]
param(
# certificate to add to p7b file
[Parameter(Mandatory = $true, ValueFromPipeline=$true)]
[Security.Cryptography.X509Certificates.X509Certificate2]$Certificate,
# path an name of the p7b container
[Parameter(Mandatory = $true)]
[IO.FileInfo]$OutputFile,
# automatically add the trust chain for each certificate. Requires the trust chain being available.
[switch]$IncludeAllCerts
)
Begin{
$certs = New-Object Security.Cryptography.X509Certificates.X509Certificate2Collection
}
Process {
if ($IncludeAllCerts) {
$chain = New-Object Security.Cryptography.X509Certificates.X509Chain
$chain.ChainPolicy.RevocationMode = "NoCheck"
[void]$chain.Build($Certificate)
$chain.ChainElements | ForEach-Object {[void]$certs.Add($_.Certificate)}
$chain.Reset()
} else {
[void]$certs.Add($Certificate)
}
}
End {
Set-Content -Path $OutputFile.FullName -Value $certs.Export("pkcs7") -Encoding Byte
}
}