ps/Modules/Alkami.PowerShell.IIS/Public/New-WebSite.ps1

78 lines
3.1 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
function New-WebSite {
<#
.SYNOPSIS
Create a new website using Alkami default patterns
.PARAMETER SitePath
The path to use for the site. Example C:\Inetpub\wwwroot
.PARAMETER Url
The URL to assign to the site. If a cert is found, will bind to 80 and 443. If no certificate is found, just port 80.
.PARAMETER AppPoolName
Optional app pool name. Useful for consolidating app pools.
#>
## TODO: cbrand ~ Can we rename this file so it doesn't conflict with "traditional" Microsoft module naming? Maybe "New-AlkamiWebSite"
[CmdletBinding()]
[OutputType([void])]
Param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$SitePath,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Url,
[Parameter(Mandatory = $false)]
[string]$AppPoolName
)
$logLead = Get-LogLeadName
if ([String]::IsNullOrEmpty($AppPoolName)) {
Write-Host "$logLead : No AppPoolName was provided. Using Url for AppPoolName"
$AppPoolName = $Url
}
$mgr = Get-IISServerManager
if ($null -ne $mgr.Sites[$Url]) {
Write-Host "$logLead : Website [$Url] already exists"
$newSite = $mgr.Sites[$Url]
} else {
Write-Host "$logLead : Creating Website [$Url]"
$httpBindingText = "*:80:$Url"
Write-Host "$logLead : Creating Site with binding [$httpBindingText] in path [$SitePath]"
$newSite = $mgr.Sites.Add($Url, "http", $httpBindingText, $SitePath)
## Removed dependecy on Alkami.Ops module
$computerStore = [System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine
$personalStore = [System.Security.Cryptography.X509Certificates.StoreName]::My
$certificate = Find-CertificateBySubjectOrSAN -Subject $Url -StoreLocation $computerStore -StoreName $personalStore
if ($null -eq $certificate) {
Write-Warning "$logLead : Unable to find a certificate with subject or SAN which matches [$Url]. The SSL Binding must be created manually."
} else {
$sslBindingText = "*:443:$Url"
Write-Host "$logLead : Creating SSL binding [$sslBindingText] using certificate [$($certificate.Subject)]"
$newSite.Bindings.Add($sslBindingText, $certificate.GetCertHash(), $personalStore, [Microsoft.Web.Administration.SslFlags]::Sni) | Out-Null
}
}
if ($null -eq $mgr.ApplicationPools[$AppPoolName]) {
Write-Host "$logLead : Creating application pool [$AppPoolName]"
$mgr.ApplicationPools.Add($AppPoolName) | Out-Null
}
Write-Host "$logLead : Setting site to use application pool [$AppPoolName]"
$newSite.ApplicationDefaults.ApplicationPoolName = $AppPoolName
Save-IISServerManagerChanges -ServerManager $mgr
# We want to make sure the application pool settings are proper even if it's not new
Write-Host "$logLead : Configuring application pool [$AppPoolName] with default values"
(Set-AlkamiWebAppPoolConfiguration $AppPoolName) | Out-Null
}
Set-Alias -name Create-WebSite -value New-WebSite