Create self signed certificate

Today i started a project what utilized IdentityServer and for that i needed a signed certificate. This post might not be the best explained, but more like a reminder for my self.

param (
    [Parameter(Mandatory=$true)][string]$subject = "My SelfSigned Certificate",
    [string]$outputPath = (Get-Item -Path ".\" -Verbose).FullName,
    [Parameter(Mandatory=$true)][string]$password
    #[string]$password = $( Read-Host "Input password, please" )
 )

$securePassword = ConvertTo-SecureString -String $password -Force -AsPlainText
$filename = ($subject.Replace(" ", "_") + ".pfx").ToLower();
New-SelfSignedCertificate -NotAfter 2030-12-31 -certstorelocation cert:\localmachine\my -dnsname $subject | out-null
$loc = Get-Location
Set-Location Cert:\LocalMachine\my
$createdCert = Get-ChildItem | where { $_.Subject -eq "CN=$subject" }
Set-Location $loc;
$thumbprint = $createdCert.Thumbprint
write-host "Creating cert '$subject'. Exporting to '$outputPath\$filename'.";
Export-PfxCertificate -cert cert:\localMachine\my\$thumbprint -FilePath $outputPath\$filename -Password $securePassword | Out-Null
write-host "Done!"

 

Leave a Comment

Your email address will not be published. Required fields are marked *