Last active
October 9, 2017 17:06
-
-
Save bzuillsmith/0304cb8b3916d8a5b5301b0a138f0f1f to your computer and use it in GitHub Desktop.
Powershell script to generate a self-signed certificate which is useful for ASP.NET Core 2.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.DESCRIPTION | |
SelfSignedCertificate Script | |
.NOTES | |
Author: Freist Li | |
Modified By: Ben Zuill-Smith | |
Usage: | |
1) Copy this script to a .ps1 file. | |
2) Modify the parameters passed to the function on the last line of the script. | |
3) Run powershell as admin (use `powershell -ExecutionPolicy ByPass -File <script-name>.ps1`) | |
4) Run the script. | |
#> | |
#Create Cert, install Cert to My, install Cert to Root, Export Cert as pfx | |
Function GenerateSelfSignedCert | |
{ | |
Param ($certcn, $password, $certfilepath) | |
#Check if the certificate name was used before | |
$thumbprintA=(dir cert:\CurrentUser\My -recurse | where {$_.Subject -match "CN=" + $certcn} | Select-Object -Last 1).Thumbprint | |
if ($thumbprintA.Length -gt 0) | |
{ | |
Write-Host "Duplicated Cert Name used" -ForegroundColor Cyan | |
return | |
} | |
else | |
{ | |
$thumbprintA = New-SelfSignedCertificate -Subject $certcn -DnsName $certcn -FriendlyName "ASP.NET Core Development" -KeyUsage DigitalSignature -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.1") -CertStoreLocation cert:\CurrentUser\My | | |
ForEach-Object{ $_.Thumbprint} | |
} | |
#If generated successfully | |
if ($thumbprintA.Length -gt 0) | |
{ | |
#query the new installed cerificate again | |
$thumbprintB=(dir cert:\CurrentUser\My -recurse | where {$_.Subject -match "CN=" + $certcn} | Select-Object -Last 1).thumbprint | |
#If new cert installed sucessfully with the same thumbprint | |
if($thumbprintA -eq $thumbprintB ) | |
{ | |
$message = $certcn + " installed into CurrentUser\My successfully with thumprint "+$thumbprintA | |
Write-Host $message -ForegroundColor Cyan | |
$mypwd = ConvertTo-SecureString -String $password -Force –AsPlainText | |
Write-Host "Exporting Certificate as .pfx file" -ForegroundColor Cyan | |
Export-PfxCertificate -FilePath $certfilepath -Cert cert:\CurrentUser\My\$thumbprintA -Password $mypwd | |
Write-Host "Importing Certificate to CurrentUser\Root" -ForegroundColor Cyan | |
Import-PfxCertificate -FilePath $certfilepath -Password $mypwd -CertStoreLocation cert:\CurrentUser\Root | |
} | |
else | |
{ | |
Write-Host "Thumbprint is not the same between new cert and installed cert." -ForegroundColor Cyan | |
} | |
} | |
else | |
{ | |
$message = $certcn + " is not created" | |
Write-Host $message -ForegroundColor Cyan | |
} | |
} | |
GenerateSelfSignedCert localhost <my-password> dev-ssl-cert.pfx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment