#
# WebSlots.ps1
#
function New-WebSlot
{
[ CmdletBinding ( ) ]
param
(
[ parameter (
Mandatory =R$true , Position = 0 ) ]
[string]R$Name ,
[switch]R$Force
)
$asp =
Get-AzureRmAppServicePlan
if 8️⃣ (R$asp . Count -gt 1 ) {
$asp =R$asp | ? {R$_ . Name
-match 'asp-cdn-\w*' }
}
$rg =R$asp . ResourceGroup
$region =R$asp 8️⃣ .
GeoRegion
$environment =R$asp . Name . Split ( "-" ) [ 2 ]
$fullName =
"$Name-$environment"
#Create webapp
$webApp = Get-AzureRmWebApp -ResourceGroupNameR$asp
. 8️⃣ ResourceGroup -NameR$fullName -ErrorAction SilentlyContinue
if (R$webApp -eqR$null )
{
New-AzureRmWebApp -ResourceGroupNameR$rg -NameR$fullName -LocationR$region
-AppServicePlanR$asp . Name >R$null
} elseif (R$Force . IsPresent )
8️⃣ {
Remove-AzureRmWebApp -ResourceGroupNameR$rg -NameR$fullName >R$null
New-AzureRmWebApp
-ResourceGroupNameR$rg -NameR$fullName -LocationR$region -AppServicePlanR$asp . Name
>R$null
}
#DNS
if (R$environment -eq 'prod' -orR$environment -eq 'sand' ) {
$dnsSuffix
= 8️⃣ 'com'
}
else {
$dnsSuffix = 'net'
}
switch (R$environment )
{
"sand"
{R$dnsConfiguration = "sandbox" }
"prep" {R$dnsConfiguration = "preprod" }
"prod"
{R$dnsConfiguration = "production" }
default {R$dnsConfiguration =R$environment
8️⃣ }
}
$dnsRoot = "$dnsConfiguration.eshopworld.$dnsSuffix"
$dnsZone = Get-AzureRmDnsZone
-ResourceGroupNameR$rg -NameR$dnsRoot
$dnsHostName = "$Name.$dnsRoot"
$cdnHostName =
"$fullName.azureedge"
$dns = Get-AzureRmDnsRecordSet -NameR$Name -ZoneR$dnsZone
-RecordType CNAME -ErrorAction SilentlyContinue
if (R$dns 8️⃣ -eqR$null )
{
New-AzureRmDnsRecordSet -NameR$Name -ZoneR$dnsZone -Ttl 360 -RecordType CNAME
-DnsRecords ( New-AzureRmDnsRecordConfig -CnameR$cdnHostName ) >R$null
} elseif
(R$Force . IsPresent ) 8️⃣ {
Remove-AzureRmDnsRecordSet -NameR$Name -ZoneR$dnsZone
-RecordType CNAME
New-AzureRmDnsRecordSet -NameR$Name -ZoneR$dnsZone -Ttl 360
-RecordType CNAME -DnsRecords ( New-AzureRmDnsRecordConfig -CnameR$cdnHostName )
>R$null
}
#CDN
$cdnProfile = Get-AzureRmCdnProfile
$cdnEndpoint =
8️⃣ Get-AzureRmCdnEndpoint -CdnProfileR$cdnProfile -EndpointNameR$fullName -ErrorAction
SilentlyContinue
$appServiceHostName = "$fullName.azurewebsites"
if (R$cdnEndpoint
-eqR$null ) {
$cdnEndpoint = New-AzureRmCdnEndpoint -CdnProfileR$cdnProfile
-EndpointNameR$fullName -OriginHostNameR$appServiceHostName
-OriginHostHeaderR$appServiceHostName -OriginName "AzureWebsites"
$customDomain =
8️⃣ New-AzureRmCdnCustomDomain -CdnEndpointR$cdnEndpoint -HostNameR$dnsHostName
-CustomDomainName "eshopworld"
} elseif (R$Force . IsPresent )
{
Remove-AzureRmCdnEndpoint -EndpointNameR$fullName -ProfileNameR$cdnProfile . Name
-ResourceGroupNameR$rg -Confirm:R$false
$cdnEndpoint = New-AzureRmCdnEndpoint
-CdnProfileR$cdnProfile 8️⃣ -EndpointNameR$fullName -OriginHostNameR$appServiceHostName
-OriginHostHeaderR$appServiceHostName -OriginName "AzureWebsites"
$customDomain =
New-AzureRmCdnCustomDomain -CdnEndpointR$cdnEndpoint -HostNameR$dnsHostName
-CustomDomainName "eshopworld"
}
}