使用脚本将一堆证书导入正确的证书存储区

我有一个p7b文件的证书集合,我想根据证书模板自动导入每个证书到正确的存储。 用脚本来做这件事的最好方法是什么?

我尝试使用certutil -addstore root Certificate.p7b ,并将所有根CA正确放置到根存储中,但如果遇到任何其他types的证书,则会返回一个错误。

我愿意使用批处理脚本,VBScript或PowerShell来完成这项任务。 谢谢!

我使用CertMgr.exe和一个简单的bat文件来导入证书。

 certmgr.exe -add -c ca.cer -s -r localMachine root >> log.txt certmgr.exe -add -c test.cer -s -r localMachine root >> log.txt certmgr.exe -add -c edu.cer -s -r localMachine root >> log.txt 

这是一个TechNet文章 ,它logging了您可以使用certmgr.exe执行哪些命令/用法

我还没有find一个脚本根据它的模板将它导入证书到正确的商店。 我想你自己写了这个剧本,因为它根本不存在。 我所发现的是一个从目录导入证书的PowerShell脚本,在命令中你必须自己指定正确的存储。 我认为这可能对你有用:

如何使用脚本函数导入安全证书。

注:要获取可用的商店名称列表,请运行以下命令:dir cert:| select展开StoreNames

示例用法:导入证书-CertFile“VeriSign_Expires-2028.08.01.cer”-StoreNames AuthRoot,Root-LocalMachine

导入证书-CertFile“VeriSign_Expires-2018.05.18.p12”-StoreNames AuthRoot-LocalMachine-CurrentUser -CertPassword密码-Verbose

dir -Path C:\ Certs -Filter * .cer | 导入证书-CertFile $ _ -StoreNames AuthRoot,Root-LocalMachine -Verbose

脚本本身:

 #requires -Version 2.0 function Import-Certificate { param ( [IO.FileInfo] $CertFile = $(throw "Paramerter -CertFile [System.IO.FileInfo] is required."), [string[]] $StoreNames = $(throw "Paramerter -StoreNames [System.String] is required."), [switch] $LocalMachine, [switch] $CurrentUser, [string] $CertPassword, [switch] $Verbose ) begin { [void][System.Reflection.Assembly]::LoadWithPartialName("System.Security") } process { if ($Verbose) { $VerbosePreference = 'Continue' } if (-not $LocalMachine -and -not $CurrentUser) { Write-Warning "One or both of the following parameters are required: '-LocalMachine' '-CurrentUser'. Skipping certificate '$CertFile'." } try { if ($_) { $certfile = $_ } $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $certfile,$CertPassword } catch { Write-Error ("Error importing '$certfile': $_ .") -ErrorAction:Continue } if ($cert -and $LocalMachine) { $StoreScope = "LocalMachine" $StoreNames | ForEach-Object { $StoreName = $_ if (Test-Path "cert:\$StoreScope\$StoreName") { try { $store = New-Object System.Security.Cryptography.X509Certificates.X509Store $StoreName, $StoreScope $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite) $store.Add($cert) $store.Close() Write-Verbose "Successfully added '$certfile' to 'cert:\$StoreScope\$StoreName'." } catch { Write-Error ("Error adding '$certfile' to 'cert:\$StoreScope\$StoreName': $_ .") -ErrorAction:Continue } } else { Write-Warning "Certificate store '$StoreName' does not exist. Skipping..." } } } if ($cert -and $CurrentUser) { $StoreScope = "CurrentUser" $StoreNames | ForEach-Object { $StoreName = $_ if (Test-Path "cert:\$StoreScope\$StoreName") { try { $store = New-Object System.Security.Cryptography.X509Certificates.X509Store $StoreName, $StoreScope $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite) $store.Add($cert) $store.Close() Write-Verbose "Successfully added '$certfile' to 'cert:\$StoreScope\$StoreName'." } catch { Write-Error ("Error adding '$certfile' to 'cert:\$StoreScope\$StoreName': $_ .") -ErrorAction:Continue } } else { Write-Warning "Certificate store '$StoreName' does not exist. Skipping..." } } } } end { } } 

来源: import证书