Skip to content

Instantly share code, notes, and snippets.

@peterneave
Created June 6, 2018 01:21
Show Gist options
  • Save peterneave/4d7393cbd3f887955f3e4fd721db91d1 to your computer and use it in GitHub Desktop.
Save peterneave/4d7393cbd3f887955f3e4fd721db91d1 to your computer and use it in GitHub Desktop.
Powershell Create IIS Web Binding
#Based on https://stackoverflow.com/a/26512480/7818494
function CreateNewBindings(
[parameter(Mandatory = $true, HelpMessage = 'Missing Argument WebsiteName')][string]$WebsiteName,
[parameter(Mandatory = $true, HelpMessage = 'Missing Argument HostHeader')][string]$HostHeader,
[parameter(Mandatory = $true, HelpMessage = 'Missing Argument CertCommonName')][string]$CertCommonName) {
Write-Host Creating new http binding $HostHeader on $WebsiteName
New-WebBinding -Name $WebsiteName -IPAddress "*" -Port 80 -Protocol http -HostHeader $HostHeader
Write-Host Creating new https binding $HostHeader on $WebsiteName with certificate $CertCommonName
New-WebBinding -Name $WebsiteName -IPAddress "*" -Port 443 -Protocol https -HostHeader $HostHeader -SslFlags 1
$Thumbprint = (Get-ChildItem -Path cert:\LocalMachine\My | Where-Object {$_.GetNameInfo("SimpleName", $false) -eq $CertCommonName}).Thumbprint
netsh http add sslcert hostnameport=$($HostHeader):443 certhash=$Thumbprint appid='{4dc3e181-e14b-4a21-b022-59fc669b0914}' certstorename=MY
}
CreateNewBindings -WebsiteName "My Website" -HostHeader "www.example.com" -CertCommonName "*.example.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment