
Tengo una colección de certificados en un archivo p7b y me gustaría importar automáticamente cada certificado al almacén correcto según la plantilla de certificado. ¿Cuál es la mejor manera de hacer esto con un guión?
Intenté usar certutil -addstore root Certificate.p7b
y eso colocará correctamente todas las CA raíz en el almacén raíz, pero devuelve un error si encuentra cualquier otro tipo de certificado.
Estoy dispuesto a utilizar scripts por lotes, vbscript o powershell para realizar esta tarea. ¡Gracias!
Respuesta1
Utilizo CertMgr.exe
un archivo bat simple para importar certificados.
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
Aquí estáun artículo de TechNetque documenta qué comandos/uso puede hacer con certmgr.exe
Respuesta2
No he encontrado una secuencia de comandos para importarlo al certificado en la tienda correcta según su plantilla. Creo que usted mismo creó ese guión porque simplemente no existe. Lo que encontré es un script de PowerShell que importa certificados desde un directorio y en el comando usted mismo debe especificar el almacén correcto. Pensé que podría serte útil:
Cómo utilizar el guión Función para importar certificados de seguridad.
NOTA: Para obtener una lista de nombres de tiendas disponibles, ejecute el siguiente comando: dir cert: | Seleccione -Expandir nombres de tiendas
Usos de ejemplo: Importar-Certificado -CertFile "VeriSign_Expires-2028.08.01.cer" -StoreNames AuthRoot, Root -LocalMachine
Importar-Certificado -CertFile "VeriSign_Expires-2018.05.18.p12" -StoreNames AuthRoot -LocalMachine -CurrentUser -CertPassword Contraseña -Detallado
dir -Path C:\Certs -Filter *.cer | Importar-Certificado -CertFile $_ -StoreNames AuthRoot, Root -LocalMachine -Detallado
Guión en sí:
#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
{ }
}
Fuente:Certificado de Importación