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()
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
{ }
}
#example
Import-Certificate -CertFile "filePath" -LocalMachine -StoreNames "root"
Import-Certificate -CertFile "filePath" -CertPassword "1234" -LocalMachine -StoreNames "MY"
本文链接: https://www.pstips.net/powershell-import-certificate.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

您好!感谢分享!有一个问题请教一下,Admin账户执行此脚本没有任何问题,就是普通用户执行此脚本后,访问需用证书的网站会报错,“无法访问此网站。网址为xxx的网页可能暂时无法连接,或者它已永久性地移动到了新网址”。请问这个问题该如何解决呢?十分感谢!