ps/Modules/Alkami.PowerShell.IIS/Public/Optimize-DefaultWebsite.ps1

111 lines
3.9 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
Function Optimize-DefaultWebsite {
<#
.SYNOPSIS
This function is used to ensure a consistent Default Web Site on a developers through production instance of IIS.
.DESCRIPTION
This function is used to ensure a consistent Default Web Site on a developers through production instance of IIS.
This function will do the following to the passed in site:
* ensure protocols and bindings are set correctly for http
* ensure protocols and bindings are set correctly for net.tcp
* add protocls and bindings if necessary for http
* add protocls and bindings if necessary for net.tcp
This function should not be used on non-default-web-site sites.
This function has no way to know if a site is valid or not for this optimization.
.PARAMETER Website
The default website being optimized
.INPUTS
We require the website we want to run against to be passed in.
.OUTPUTS
Returns the updated website once optimized.
.EXAMPLE
Optimize-DefaultWebsite $website
Optimize-DefaultWebsite $website
#>
[CmdletBinding(DefaultParameterSetName = 'WebsiteName')]
param(
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'WebsiteObject')]
[ValidateNotNull()]
[Object]$Website,
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'WebsiteName')]
[ValidateNotNull()]
[string]$WebsiteName
)
$loglead = (Get-LogLeadName)
if ($PSCmdlet.ParameterSetName -eq 'WebsiteObject') {
$WebsiteName = $Website.Name
}
$foundNetTcp = $false
$foundHttp = $false
$foundSimpleHttpBinding = $false
$foundWildcardHttpBinding = $false
## Cannot add duplicate collection entry of type 'binding' with combined key attributes 'protocol, bindingInformation' respectively set to 'net.tcp, 808:*'
$bindingCollection = (Get-ItemProperty IIS:\Sites\$($WebsiteName) -Name bindings).Collection
foreach ($binding in $bindingCollection) {
if ($binding.protocol -eq 'net.tcp') {
if ($binding.bindingInformation -ne '808:*') {
throw "$loglead : Found a net.tcp binding against a non-default port (not 808). Can not continue. Please contact Alkami SDK Support for more details."
}
$foundNetTcp = $true
}
if ($binding.protocol -eq 'http') {
if ($binding.bindingInformation -eq '*:80:*') {
$foundWildcardHttpBinding = $true
}
if ($binding.bindingInformation -eq '*:80:') {
$foundSimpleHttpBinding = $true
}
$foundHttp = $true
}
}
$protocolUpdateRequired = $false
$protocols = (Get-ItemProperty IIS:\Sites\$($WebsiteName) -Name EnabledProtocols).enabledProtocols
if ($protocols -notmatch 'http') {
Write-Verbose "$loglead : New protocol http"
$protocols = 'http,' + $protocols
$protocolUpdateRequired = $true
}
if ($protocols -notmatch 'net.tcp') {
Write-Verbose "$loglead : New protocol net.tcp"
$protocols += ',net.tcp'
$protocolUpdateRequired = $true
}
## the above may force a double comma because this is easier for me to do
$protocols = $protocols -replace ',,',','
if (!$foundNetTcp) {
Write-Verbose "$loglead : Set bindings net.tcp"
(New-ItemProperty IIS:\Sites\$($WebsiteName) -Name bindings -Value @{protocol="net.tcp"; bindingInformation="808:*"} -ErrorAction Ignore) | Out-Null
}
if (!$foundHttp) {
Write-Verbose "$loglead : New bindings http"
(New-ItemProperty IIS:\Sites\$($WebsiteName) -Name bindings -Value @{protocol="http"; bindingInformation="*:80:"} -ErrorAction Ignore) | Out-Null
}
if ($foundSimpleHttpBinding -and $foundWildcardHttpBinding) {
## TODO: Do we need to remove one or the other?
}
if ($protocolUpdateRequired) {
Write-Verbose "$loglead : Set EnabledProtocols"
(Set-ItemProperty IIS:\Sites\$($WebsiteName) -Name EnabledProtocols -Value $protocols -ErrorAction Ignore) | Out-Null
}
(Set-ItemProperty "IIS:\Sites\$($WebsiteName)" -Name applicationDefaults.preloadEnabled -Value $true) | Out-Null
return (Get-Website -Name $WebsiteName)
}