Last active
November 8, 2018 22:48
-
-
Save achingono/b2913afb330c8501ac5d22ec8cd058b9 to your computer and use it in GitHub Desktop.
Update IIS bindings from sitecore SiteDefinitions file using powershell
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
param( | |
[Parameter(Position=0,Mandatory=$true)] | |
[Alias("Site")] | |
$siteName, | |
[Parameter(Position=1,Mandatory=$true)] | |
$domain, | |
[Parameter(Position=2)] | |
$configPath = "App_Config\Include\zzz\Project.SiteDefinitions.config" | |
[Parameter(Position=3)] | |
$xpath = "/configuration/sitecore/sites/site" | |
) | |
if($null -eq $siteName -or $siteName -eq "") | |
{ | |
$siteName = $($env:WebsiteName) | |
} | |
if($null -eq $domain -or $domain -eq "") | |
{ | |
$domain = $($env:Domain) | |
} | |
if($null -eq $siteName -or $siteName -eq "") | |
{ | |
Write-Error "SiteName parameter is required" | |
} | |
if($null -eq $domain -or $domain -eq "") | |
{ | |
Write-Error "Domain parameter is required" | |
} | |
Import-Module WebAdministration | |
$rootPath = Get-ItemProperty "IIS:\Sites\$($siteName)" -Name physicalPath | |
Write-Host "Physical path for site: '$($siteName)' is: '$($rootPath)'" | |
$certificate = Get-ChildItem -Path Cert:\LocalMachine\My | where-Object {$_.subject -like "*$domain*"} | |
Write-Host "Certificate thumbprint for domain: '$($domain)' is: '$($certificate.Thumbprint)'" | |
$path = Join-Path $rootPath $configPath | |
Write-Host "Loading '$path'" | |
[xml]$xml = Get-Content $path | |
$nodes = $xml.SelectNodes($xpath) | |
foreach($node in $nodes) | |
{ | |
$header = $node.Attributes["targetHostName"].Value | |
if ($null -eq (Get-WebBinding -name $siteName -Protocol http -HostHeader $header)) | |
{ | |
New-WebBinding -name $siteName -Protocol http -HostHeader $header -IPAddress "*" -Port 80 | |
Write-Host "Created HTTP binding for: '$header'" | |
} | |
else { | |
Write-Host "HTTP Binding for: '$header' already exists" | |
} | |
if ($null -eq (Get-WebBinding -name $siteName -Protocol https -HostHeader $header)) | |
{ | |
New-Item -Path "IIS:\SslBindings\*!443!${header}" -Thumbprint $($certificate.Thumbprint) -SSLFlags 1 | |
Write-Host "Created SSL binding for: '$header'" | |
New-WebBinding -name $siteName -Protocol https -HostHeader $header -IPAddress "*" -Port 443 -SslFlags 1 | |
Write-Host "Created HTTPS binding for: '$header'" | |
} | |
else { | |
Write-Host "HTTPS Binding for: '$header' already exists" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment