-
-
Save techknowhow/97e771fc1e9f1ffbcf05065048181e6e to your computer and use it in GitHub Desktop.
Powershell script to enable ssl on a website using Self Signed Certificate.
This file contains 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
$hostName="www.techknowhow.net" | |
$iisSite="techknowhow" | |
New-SelfSignedCertificate -DnsName $hostName -CertStoreLocation "cert:\LocalMachine\My" | |
$SourceStoreScope = 'LocalMachine' | |
$SourceStorename = 'My' | |
$SourceStore = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Store -ArgumentList $SourceStorename, $SourceStoreScope | |
$SourceStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadOnly) | |
$cert = $SourceStore.Certificates | Where-Object -FilterScript { | |
$_.subject -like "*$hostName*" | |
} | |
$cert | |
$DestStoreScope = 'LocalMachine' | |
$DestStoreName = 'root' | |
$DestStore = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Store -ArgumentList $DestStoreName, $DestStoreScope | |
$DestStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite) | |
$DestStore.Add($cert) | |
$certThumbprint = ($DestStore.Certificates | Where-Object -FilterScript { | |
$_.subject -like "*$hostName*" | |
} | Select-Object -First 1).Thumbprint | |
$SourceStore.Close() | |
$DestStore.Close() | |
"Cert Hash: " + $certThumbprint | |
# http.sys mapping of ip/hostheader to cert | |
$guid = [guid]::NewGuid().ToString("B") | |
netsh http add sslcert hostnameport="${hostName}:443" certhash=$certThumbprint certstorename=root appid="$guid" | |
# iis site mapping ip/hostheader/port to cert - also maps certificate if it exists | |
# for the particular ip/port/hostheader combo | |
New-WebBinding -name $iisSite -Protocol https -HostHeader $hostName -Port 443 -SslFlags 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment