Skip to content

Instantly share code, notes, and snippets.

@michaelkc
Created October 3, 2023 09:27
Show Gist options
  • Save michaelkc/fc390611ef5ec58ba44520adc143a25a to your computer and use it in GitHub Desktop.
Save michaelkc/fc390611ef5ec58ba44520adc143a25a to your computer and use it in GitHub Desktop.
Generate a self signed cert in passwordless pfx and Base64 cer formats (e.g. for Azure service principal authn)
param (
[Parameter(Mandatory = $true)]
[string]$certName
)
$validFrom = Get-Date -Year 2020 -Month 1 -Day 1
$validTo = Get-Date -Year 2099 -Month 1 -Day 1
$cert = New-SelfSignedCertificate -DnsName $certName -CertStoreLocation "Cert:\CurrentUser\My" -KeyExportPolicy Exportable -KeyProtection None -KeyUsage DigitalSignature,KeyEncipherment -NotBefore $validFrom -NotAfter $validTo
$thumbprint = $cert.Thumbprint
Export-PfxCertificate -Cert "Cert:\CurrentUser\My\$thumbprint" -FilePath "$($certName)_private.pfx" -Password (new-object System.Security.SecureString) -Force | Out-Null
Export-Certificate -Cert "Cert:\CurrentUser\My\$thumbprint" -FilePath "$($certName)_public.der" -Type Cert -Force | Out-Null
Remove-Item -Path "Cert:\CurrentUser\My\$thumbprint" -Force | Out-Null
certutil -encode -f "$($certName)_public.der" "$($certName)_public.cer" | Out-Null
ls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment