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

58 lines
1.9 KiB
PowerShell

function New-DefaultWebsite {
<#
.SYNOPSIS
Creates a site called "Default Web Site"
.DESCRIPTION
When called, will attempt to force-create a site called "Default Web Site"
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)]
[Alias("Name")]
[string]$DefaultWebsiteName = "Default Web Site"
)
$loglead = (Get-LogLeadName)
$returnWebsite = (Get-Website -Name $DefaultWebsiteName)
if ($null -ne $returnWebsite) {
Write-Error "$logLead : A site called [$DefaultWebsiteName] already exists"
return $returnWebsite
}
if ([string]::IsNullOrWhiteSpace($DefaultWebsiteName)) {
Write-Error "$logLead : no site name was passed into this function"
}
Write-Verbose "$loglead : Using logic for force-creating `"Default Web Site`" with name '$DefaultWebsiteName'"
$appPoolName = "DefaultAppPool"
if ($DefaultWebsiteName -ne "Default Web Site") {
$appPoolName = $DefaultWebsiteName
}
$appPool = (Get-Item (Join-Path "IIS:\AppPools" $appPoolName))
if ($null -eq $appPool) {
## Couldn't find the application pool for this app, need to create one to go with this application
$appPool = New-AlkamiWebAppPool -Name $appPoolName
}
$physicalPath = (Get-DefaultWebsiteDefaultPath)
if (!(Test-Path $physicalPath)) {
Write-Verbose "$logLead : $physicalPath not found. Creating."
(New-Item -ItemType Directory -Path $physicalPath) | Out-Null
}
Write-Host "Building the site [$DefaultWebsiteName] now in IIS"
## Unfortunately there is an Alkami function with the exact same name.
$returnWebsite = WebAdministration\New-Website $DefaultWebsiteName -Force -IPAddress * -PhysicalPath $physicalPath -Port 80 -ApplicationPool $appPoolName
## Go ahead and optimize it since we just created it
Optimize-DefaultWebsite $returnWebsite
return $returnWebsite
}