Created
November 20, 2017 16:14
-
-
Save ukcoderj/db55b359e11d06d8d53dc49a85d4b1f0 to your computer and use it in GitHub Desktop.
Powershell - Create a self-signed certificate and create an IIS binding for the website
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
Clear-Host | |
$certificateDnsName = 'my.localcert.ssl' # a name you want to give to your certificate (can be anything you want for localhost) | |
$siteName = "Default Web Site" # the website to apply the bindings/cert to (top level, not an application underneath!). | |
$fqdn = "" #fully qualified domain name (empty for 'All unassigned', or e.g 'contoso.com') | |
# ---------------------------------------------------------------------------------------- | |
# SSL CERTIFICATE CREATION | |
# ---------------------------------------------------------------------------------------- | |
# create the ssl certificate that will expire in 2 years | |
$newCert = New-SelfSignedCertificate -DnsName $certificateDnsName -CertStoreLocation cert:\LocalMachine\My -NotAfter (Get-Date).AddYears(2) | |
"Certificate Details:`r`n`r`n $newCert" | |
# ---------------------------------------------------------------------------------------- | |
# IIS BINDINGS | |
# ---------------------------------------------------------------------------------------- | |
$webbindings = Get-WebBinding -Name $siteName | |
$webbindings | |
$hasSsl = $webbindings | Where-Object { $_.protocol -like "*https*" } | |
if($hasSsl) | |
{ | |
Write-Output "ERROR: An SSL certificate is already assigned. Please remove it manually before adding this certificate." | |
Write-Output "Alternatively, you could just use that certificate (provided it's recent/secure)." | |
} | |
else | |
{ | |
"Applying TLS/SSL Certificate" | |
New-WebBinding -Name $siteName -Port 443 -Protocol https -HostHeader $fqdn | |
(Get-WebBinding -Name $siteName -Port 443 -Protocol "https" -HostHeader $fqdn).AddSslCertificate($newCert.Thumbprint, "my") | |
"`r`n`r`nNew web bindings" | |
$webbindings = Get-WebBinding -Name $siteName | |
$webbindings | |
} | |
"`r`n`r`nSSL Assignment Complete" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment