Powershell 从命令行将pfx文件导入特定证书存储

Powershell 从命令行将pfx文件导入特定证书存储,powershell,certificate,certutil,Powershell,Certificate,Certutil,使用CertUtil从pfx文件将证书导入用户的个人存储相对容易: certutil –f –p [certificate_password] –importpfx C:\[certificate_path_and_name].pfx 但这最终会出现在当前用户的个人存储中。我需要信任本地机器上的人 是否有任何方法可以从命令行执行此操作,或者使用另一个certutil命令或其他实用程序调用certutil importpfx上的不同参数?Powershell是另一种可能性,尽管我对它知之甚少

使用CertUtil从pfx文件将证书导入用户的个人存储相对容易:

certutil –f –p [certificate_password] –importpfx C:\[certificate_path_and_name].pfx 
但这最终会出现在当前用户的个人存储中。我需要信任本地机器上的人

是否有任何方法可以从命令行执行此操作,或者使用另一个certutil命令或其他实用程序调用certutil importpfx上的不同参数?Powershell是另一种可能性,尽管我对它知之甚少

干杯, Matt

检查以下链接:

进口证明:

您可以执行以下操作:

dir -Path C:\Certs -Filter *.cer | Import-Certificate -CertFile $_ -StoreNames AuthRoot, Root -LocalMachine -Verbose

将我的发现锚定在这里供未来读者阅读

将证书导入到本地计算机上受信任的根证书颁发机构:

CERTUTIL -addstore -enterprise -f -v root "somCertificat.cer"
certutil -f -user -p oracle -enterprise -importpfx root "example.pfx"
certutil -f -user -p oracle -enterprise -importpfx TrustedPeople "example.pfx"
将pfx导入本地计算机上的个人

CERTUTIL -f -p somePassword -importpfx "somePfx.pfx"
Certutil -addstore -f "TRUSTEDPEOPLE" "someCertificate.cer"
将pfx导入到本地计算机上的受信任人员-导入到importpfx.exe

importpfx.exe -f "somePfx.pfx" -p "somePassword" -t MACHINE -s "TRUSTEDPEOPLE"
将证书导入到本地计算机上受信任的人员

CERTUTIL -f -p somePassword -importpfx "somePfx.pfx"
Certutil -addstore -f "TRUSTEDPEOPLE" "someCertificate.cer"

对于其他正在寻找此文件的人,我无法将
certutil-importpfx
使用到特定的存储中,并且我不想下载jaspernygaard的答案提供的importpfx工具,以避免将文件复制到大量服务器的要求。我最终在显示的powershell脚本中找到了答案

代码使用
System.Security.Cryptography.X509Certificates
导入证书,然后将其移动到所需的存储:

function Import-PfxCertificate { 

    param([String]$certPath,[String]$certRootStore = “localmachine”,[String]$certStore = “My”,$pfxPass = $null) 
    $pfx = new-object System.Security.Cryptography.X509Certificates.X509Certificate2 

    if ($pfxPass -eq $null) 
    {
        $pfxPass = read-host "Password" -assecurestring
    } 

    $pfx.import($certPath,$pfxPass,"Exportable,PersistKeySet") 

    $store = new-object System.Security.Cryptography.X509Certificates.X509Store($certStore,$certRootStore) 
    $store.open("MaxAllowed") 
    $store.add($pfx) 
    $store.close() 
}
有了Windows 2012 R2(Win 8.1)和更高版本,您还可以使用“官方”

以下是代码的一些基本部分(一个可修改的示例):

基于mao47的代码和一些研究,我写了一篇小文章和一个简单的cmdlet,用于将PFX证书导入/推送到远程计算机


我的文章提供了更多详细信息和完整代码,这些代码也适用于PSv2(服务器2008 R2/Windows 7上的默认设置),只要您启用了SMB和管理共享访问。

以下是完整代码、导入pfx、添加iis网站、添加ssl绑定:

$SiteName = "MySite"
$HostName = "localhost"
$CertificatePassword = '1234'
$SiteFolder = Join-Path -Path 'C:\inetpub\wwwroot' -ChildPath $SiteName
$certPath = 'c:\cert.pfx'


Write-Host 'Import pfx certificate' $certPath
$certRootStore = “LocalMachine”
$certStore = "My"
$pfx = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$pfx.Import($certPath,$CertificatePassword,"Exportable,PersistKeySet") 
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store($certStore,$certRootStore) 
$store.Open('ReadWrite')
$store.Add($pfx) 
$store.Close() 
$certThumbprint = $pfx.Thumbprint


Write-Host 'Add website' $SiteName
New-WebSite -Name $SiteName -PhysicalPath $SiteFolder -Force
$IISSite = "IIS:\Sites\$SiteName"
Set-ItemProperty $IISSite -name  Bindings -value @{protocol="https";bindingInformation="*:443:$HostName"}
if($applicationPool) { Set-ItemProperty $IISSite -name  ApplicationPool -value $IISApplicationPool }


Write-Host 'Bind certificate with Thumbprint' $certThumbprint
$obj = get-webconfiguration "//sites/site[@name='$SiteName']"
$binding = $obj.bindings.Collection[0]
$method = $binding.Methods["AddSslCertificate"]
$methodInstance = $method.CreateInstance()
$methodInstance.Input.SetAttributeValue("certificateHash", $certThumbprint)
$methodInstance.Input.SetAttributeValue("certificateStoreName", $certStore)
$methodInstance.Execute()
对于Windows 10:

将证书导入到当前用户的受信任根证书颁发机构:

certutil -f -user -p oracle -importpfx root "example.pfx"
certutil -f -user -p oracle -importpfx TrustedPeople "example.pfx"
将证书导入到当前用户的受信任人员:

certutil -f -user -p oracle -importpfx root "example.pfx"
certutil -f -user -p oracle -importpfx TrustedPeople "example.pfx"
将证书导入到本地计算机上受信任的根证书颁发机构:

CERTUTIL -addstore -enterprise -f -v root "somCertificat.cer"
certutil -f -user -p oracle -enterprise -importpfx root "example.pfx"
certutil -f -user -p oracle -enterprise -importpfx TrustedPeople "example.pfx"
将证书导入到本地计算机上受信任的人员:

CERTUTIL -addstore -enterprise -f -v root "somCertificat.cer"
certutil -f -user -p oracle -enterprise -importpfx root "example.pfx"
certutil -f -user -p oracle -enterprise -importpfx TrustedPeople "example.pfx"

在较新版本的windows中,Certuil具有[CertificateStoreName],我们可以在其中提供存储名称。在早期版本的windows中,这是不可能的

安装*.pfx证书: certutil-f-p”“-enterprise-ImportFX根目录”

安装*.cer证书: certutil-addstore-enterprise-f-v root“”

有关详细信息,可以在windows cmd中执行以下命令。 C:>certutil-importpfx-? 用法:
CertUtil[Options]-importPFX[CertificateStoreName]PFX文件[Modifiers]

CertUtil无法将PFX导入受信任的人,这令人沮丧。CertUtil可以与远程PSSession(PowerShell)配合使用,但ImportFX不能(仅供参考,ImportFX的源代码为)。我不确定CERTUTIL有什么不同之处,但它确实适用于远程PS会话,但我无法将cert放在受信任的人中。唉。这是非常令人沮丧的几天。我们最终编写了一组powershell函数,来完成这项艰巨的工作。查看CiPsLib.Certificates.psm1->导入证书如何在不使用密码的情况下导入?有可能吗?@drgmak,如果证书受空密码保护,请使用-p“”。如果它有密码保护,你需要知道密码。你能帮我理解值及其含义吗。“MaxAllowed”、“My”、@RaviKhambhati:My是我正在使用的证书存储的名称。有关证书存储位置的更多信息,请参阅。MaxAllowed是我用来打开的OpenFlags的值。老实说,我只是复制并粘贴了那个部分,但您可以在这里了解更多关于它可能的值:非常感谢。当我们从IIS执行相同的操作时,这些值是多少?虽然这可能从理论上回答了这个问题,但在这里包括答案的基本部分,并提供链接供参考。我真的很难将用户证书添加到新的存储中。最后一个例子对我有效。注意:如果您使用门店名称(例如“ABC”)而不是“TrustedPeople”,则将创建门店!不需要使用-addstore参数来添加存储…这是我一直坚持的问题。您可能希望导入标志为
“Exportable,MachineKeySet,PersistKeySet”
,以便将私钥放入machinekeys,而不是当前用户的配置文件中。