Powershell 导入证书 1


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
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

一条评论 “Powershell 导入证书

  • 高风

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