Skip to content

Instantly share code, notes, and snippets.

@techdecline
Created August 18, 2020 06:32
Show Gist options
  • Select an option

  • Save techdecline/af31b8a41b40c1a25e9fb93308552bf6 to your computer and use it in GitHub Desktop.

Select an option

Save techdecline/af31b8a41b40c1a25e9fb93308552bf6 to your computer and use it in GitHub Desktop.
param (
[Parameter(Mandatory)]
[String]$Pkcs12String,
[Parameter(Mandatory=$false)]
[ValidateSet("CurrentUser","LocalMachine")]
[String]$CertificateStoreLocation = "LocalMachine",
[Parameter(Mandatory=$false)]
[String]$CertificateStoreName = "My"
)
# Write Certificate to temporary file
$certFile = New-TemporaryFile
$Pkcs12String | Out-File $certFile.FullName
# Create certificate object
$pfx = new-object System.Security.Cryptography.X509Certificates.X509Certificate2
# Import certificate file into certificate object
$pfx.Import($certFile.FullName, $null, "Exportable,PersistKeySet")
# Add certificate to selected store
$store = new-object System.Security.Cryptography.X509Certificates.X509Store($CertificateStoreName,$CertificateStoreLocation)
$store.open("MaxAllowed")
$store.add($pfx)
$store.close()
# Cleanup temporary file
Remove-Item $certFile
return $pfx.Thumbprint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment