版权声明:authored by zzubqh https://blog.csdn.net/qq_36810544/article/details/81773838
身边都是程序猿,会有种天下所有人的计算机知识都是这个水平的错觉。客户的计算机知识还是要充分考虑的,真的没法去给他们解释“受信任的颁发者”和”个人存储区”到底要怎么选,他们也不想知道,最好的办法就是简单暴力有效的让他们直接执行一个脚本完事,连”下一步”都不用点了
################################################################################
# 执行说明 #
# 在windows下调出“运行”对话框(win + r),输入"powershell" #
# 参数说明: #
# CertFilePath: 文件夹内.pfx文件名,带扩展名 #
# CertPwd: .pfx文件的密码 #
# 在powershell终端下输入:.\cert_step.ps1 -CertFilePath "xxx.pfx" -CertPwd "xxx" #
# 例: .\cert_step.ps1 -CertFilePath "test_cert.pfx" -CertPwd "5tpH5zhL" #
#################################################################################
param
(
[string] $CertFilePath = $(throw "Paramerter -CertFilePath [System.String] is required."),
[string] $CertPwd = $(throw "Paramerter -CertPwd [System.String] is required.")
)
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,
[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags] $StorageFlag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable
)
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,$StorageFlag )
}
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()
"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()
"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
{ }
}
$CurrentyDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
Import-Certificate -CertFile "$CurrentyDir\$CertFilePath" -CertPassword "$CertPwd" -LocalMachine -StoreNames "My"