ps/Modules/Alkami.PowerShell.IIS/Public/Get-DefaultWebsite.ps1
2023-05-30 22:51:22 -07:00

57 lines
2.4 KiB
PowerShell

Function Get-DefaultWebsite {
<#
.SYNOPSIS
Get the default website (typically "Default Web Site")
.DESCRIPTION
This function will try to find the first site with a binding protocol of http and bound to port 80 (against localhost, 127.0.0.1, or *:80).
If no site is found, will return a $null. In that case run New-DefaultWebsite
Recommendation is to run the result through Optimize-DefaultWebsite which will return the object as well.
#>
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSPossibleIncorrectComparisonWithNull", "", Justification="Array Consolidation is Acceptable")]
Param(
[Parameter(Mandatory=$false)]
[string]$DefaultWebSiteName = "Default Web Site"
)
$loglead = (Get-LogLeadName)
$returnWebsite = Get-Website -Name $DefaultWebSiteName
# TODO: This pattern is likely to break in the future as we add more and more "non-default" sites.
# The goal of this function is to not-break for SDK clients partner developers.
$definitelySkipSiteNames = @('Client','Admin','WebClient','WebClientAdmin','IPSTS','IP-STS','Orion','Eagle Eye','CoreDashboard')
if ($null -eq $returnWebsite){
$eligibileSites = (Get-ChildItem IIS:\Sites)
$sites = @()
$potentialBindings = @("*:80:","127.0.0.1:80:","localhost:80:")
foreach($site in $eligibileSites) {
if ($null -ne $definitelySkipSiteNames.Where({$_ -match $site.Name})) {
Write-Host "$logLead : Site name [$($site.Name)] matched a definite-skip pattern. Ignoring and moving to the next."
continue
}
$siteBindings = $site.Bindings.Collection
foreach($binding in $siteBindings) {
$potentialBinding = ($binding.bindingInformation -split ' ')
if (($binding.protocol -eq 'http') -and $null -ne ($potentialBindings -contains $potentialBinding)[0]) {
$sites += $site;
}
}
}
## Get the site with the lowest ID
$sites = @(($sites, @() -ne $null)[0] | Sort-Object ID)
Write-Verbose "$logLead : Falling back to array lookup for final value. Site count: $($sites.Length). Taking first record: [$($sites[0])]"
$returnWebsite = $sites[0];
} else {
Write-Verbose "$logLead : Found '$DefaultWebSiteName' already existed"
}
return $returnWebsite;
}