pfx證書自動安裝powershell指令碼
阿新 • • 發佈:2018-11-08
身邊都是程式猿,會有種天下所有人的計算機知識都是這個水平的錯覺。客戶的計算機知識還是要充分考慮的,真的沒法去給他們解釋“受信任的頒發者”和”個人儲存區”到底要怎麼選,他們也不想知道,最好的辦法就是簡單暴力有效的讓他們直接執行一個指令碼完事,連”下一步”都不用點了
################################################################################
# 執行說明 #
# 在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"