powershell PowerShell:导入证书

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell PowerShell:导入证书相关的知识,希望对你有一定的参考价值。

<#requires -Version 2.0
Function to import security certificates.

NOTE: To get a list of available store names, run the following command: 
dir cert: | Select -Expand StoreNames

Example Usages:
Import-Certificate -CertFile “VeriSign_Expires-2028.08.01.cer” -StoreNames AuthRoot, Root -LocalMachine

Import-Certificate -CertFile “VeriSign_Expires-2018.05.18.p12” -StoreNames AuthRoot -LocalMachine -CurrentUser -CertPassword Password -Verbose

dir -Path C:\Certs -Filter *.cer | Import-Certificate -CertFile $_ -StoreNames AuthRoot, Root -LocalMachine -Verbose
#>

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 = $_.FullName
            }
            $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
{ }
}

Import-Certificate -CertFile 'F:\Applic\Certificaten\SR001247\KASBANK-Root-CA.cer' -StoreNames Root -LocalMachine -CurrentUser
Import-Certificate -CertFile 'F:\Applic\Certificaten\SR001247\Kas_Pol_Prod_CA(2).cer' -StoreNames CA -LocalMachine -CurrentUser
Import-Certificate -CertFile 'F:\Applic\Certificaten\SR001247\Cust_Prod_CA(1).crt' -StoreNames CA -LocalMachine -CurrentUser

以上是关于powershell PowerShell:导入证书的主要内容,如果未能解决你的问题,请参考以下文章

powershell Powershell命令用于导出Azure AD并导入到本地AD

Powershell模块导入方法

Powershell 批量导入AD账户

SharePoint PowerShell 从CSV文件导入数据到列表

powershell 此powershell将列表导出到已删除的文件。第二个文件将数据导入另一个列表。

使用 Powershell 将大型 CSV 批量导入 SQL Server