Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- [CmdletBinding()]
- param(
- # a name you want to give to your certificate (can be anything you want for localhost)
- [Parameter(Mandatory=$True,Position=1)]
- [ValidateNotNullOrEmpty()]
- $dnsName = "localhost",
- #the website to apply the bindings/cert to (top level, not an application underneath!).
- [Parameter(Mandatory=$True,Position=2)]
- [ValidateNotNullOrEmpty()]
- $siteName = "Default Web Site",
- #fully qualified domain name (empty for 'All unassigned', or e.g 'contoso.com')
- [Parameter(Mandatory=$False,Position=3)]
- [ValidateNotNullOrEmpty()]
- $fqdn = ""
- )
- Clear-Host
- # ----------------------------------------------------------------------------------------
- # SSL CERTIFICATE CREATION
- # ----------------------------------------------------------------------------------------
- # create the ssl certificate that will expire in 2 years
- $newCert = New-SelfSignedCertificate -DnsName $dnsName -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"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.