Pages

7/25/2022

Generate Self-Signed Certificate with PowerShell

It's been a long time, but I'm back. Here is a sample script to generate a self-signed certificate. This was helpful for building API access to Adobe Cloud User Management API


 ### Create Self-Signed Certificate (only works Windows 8.1, 10, Server 2016+ ###

$Name           = "MyCert"
$ExportPath     = "C:\Data"           # Path to export cert
$Password       = "MySecretPassword"  # Passphrase on cert
$CertTtlMonths  = 24                  # Lifespan of the cert in months
 
$SecurePwd      = ConvertTo-SecureString -String $Password -Force -AsPlainText
$Certificate    = New-SelfSignedCertificate -DnsName $Name -CertStoreLocation "cert:\LocalMachine\My" -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" -HashAlgorithm "SHA256" -NotAfter (Get-Date).AddYears($CertTtlMonths)
Get-ChildItem -Path ("cert:\localMachine\My\" + $Certificate[0].Thumbprint ) | Export-PfxCertificate -FilePath "$ExportPath\MyCert.pfx" -Password $SecurePwd
  
  
### Export Public Key (Required Server 2016+) ###
Export-Certificate -FilePath "$ExportPath\MyCert.cer" -Cert $Certificate -Type CERT -NoClobber
CertUtil -Encode "$ExportPath\MyCert.cer" "$ExportPath\MyCertB64.crt"

No comments:

Post a Comment