从 Windows 命令行导入 .cer 证书

Ant*_*lev 5 certificate command-line-interface

厌倦了穿越 MMC 丛林,我需要一种方法将给定的.cer文件(仅包含一个公钥)导入机器范围的证书存储区(不知道它是如何用英语调用的,因为我使用的是本地化版本的Windows)进入“个人证书”文件夹。两者cmd.exe或 PowerShell 版本都可以。

Mas*_*imo 4

Powershell解决方案,使用System.Security.Cryptograpy.X509Certificates

$filename = "MyFileName.cer"

[Reflection.Assembly]::Load("System.Security, Version=2.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a")

$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($filename)

$store = New-Object System.Security.Cryptography.X509Certificates.X509Store([System.Security.Cryptography.X509Certificates.StoreName]::My,[System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine)

$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite);

$store.Add($cert);
Run Code Online (Sandbox Code Playgroud)

对于所有完整的命名空间规范,我们深表歉意,但 PowerShell 中没有“using”指令。