
p7b ファイルに証明書のコレクションがあり、証明書テンプレートに応じて各証明書を適切なストアに自動的にインポートしたいと考えています。スクリプトを使用してこれを行う最適な方法は何ですか?
を使用しようとしましたcertutil -addstore root Certificate.p7b
が、すべてのルート CA がルート ストアに正しく配置されますが、他の種類の証明書に遭遇するとエラーが返されます。
このタスクを実行するために、バッチ スクリプト、VBScript、または PowerShell を使用するつもりです。ありがとうございます。
答え1
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の記事certmgr.exe で実行できるコマンドや使用法を文書化します。
答え2
テンプレートに基づいて正しいストアに証明書をインポートするスクリプトが見つかりませんでした。そのスクリプトは存在しないので、自分で作成したと思います。私が見つけたのは、ディレクトリから証明書をインポートする PowerShell スクリプトで、コマンドで正しいストアを自分で指定する必要があります。役に立つかもしれないと思いました:
スクリプトの使い方 セキュリティ証明書をインポートする機能。
注: 使用可能なストア名のリストを取得するには、次のコマンドを実行します: dir cert: | Select -Expand StoreNames
使用例: Import-Certificate -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 | Import-Certificate -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
{ }
}
ソース:輸入証明書