Param (
[Parameter()][String]$location,
[Parameter()][String]$name,
[Parameter()][String]$env)
$name= 'pav'
$location= 'westeurope'
$env = "master"
$tenantId= '00000000-0000-00000-00000-0000000000000'
$subscriptionId= '000000000-00000-0000-0000-000000000000'
#create resource group
function CreateResourceGroup {
Param(
[Parameter(Mandatory=$true)][String]$name,
[Parameter(Mandatory=$true)][String]$location,
[Parameter(Mandatory=$true)][String]$env)
#setup resource Group name using prefix
$rgName = ("rg-$name-$env").ToLower()
#check if resource group already exists
$resourceGroup = Get-AzResourceGroup `
-Name $rgName `
-ErrorAction SilentlyContinue
if($resourceGroup){
Write-host "Resource group '$rgName' already exists" -foregroundcolor yellow
return $rgName
}
#create resource group
try {
Write-Host "Createing '$rgName' resource group in '$location'" -ForegroundColor Magenta
$resourceGroup = New-AzResourceGroup `
-Name $rgName `
-Location $location
Write-host "Resource group '$rgName' created in '$location'" -foregroundcolor Green
return $rgName
}
catch {
#failed to create resourcec group
Write-error "Resource group '$rgName' NOT created" -ErrorAction Stop
}
}
#create Blob storage account
function CreateStorageAcccount {
param (
[Parameter(Mandatory=$true)][String]$name,
[Parameter(Mandatory=$true)][String]$rgName,
[Parameter(Mandatory=$true)][String]$location,
[Parameter(Mandatory=$true)][String]$env)
$stName = "st$name$env";
Write-Host "Create storage account with $stName $rgName $env"
$storageAccount = Get-AzStorageAccount -ResourceGroupName $rgName -Name $stName -ErrorAction SilentlyContinue
if($storageAccount){
Write-host "Sorage account '$stName' already exists in resouce group '$rgName'" -foregroundcolor yellow
return $stName
}
#create storage account
try {
Write-Host "Createing storage account '$stName' in resource group in '$rgname'" -ForegroundColor Magenta
$storageAccount = New-AzStorageAccount `
-ResourceGroupName $rgName `
-Name $stName `
-Location $location `
-SkuName Standard_LRS `
-Kind BlobStorage `
-AccessTier Hot
Write-host "Storage account created" -foregroundcolor Green
return $stName
}
catch {
#failed to create storagte account
Write-error "storage account '$stName' NOT created in resouce group '$rgName'" -ErrorAction Stop -foregroundcolor red
}
}
#create Blob storage container
function CreateStorageContainer {
param (
[Parameter(Mandatory=$true)][String]$name,
[Parameter(Mandatory=$true)][String]$rgName,
[Parameter(Mandatory=$true)][String]$env,
[Parameter(Mandatory=$true)][String]$stName)
$strContainerName = "container-public-$name-$env".ToLower()
Write-Host "$strContainerName $rgName $env $stName"
$key = Get-AzStorageAccountKey `
-ResourceGroupName $rgName `
-Name $stName
$stContext = New-AzStorageContext `
-StorageAccountName $stName `
-StorageAccountKey $key[0].Value
$strContainer = Get-AzStorageContainer `
-Name $strContainerName `
-Context $stContext `
-ErrorAction SilentlyContinue
if($strContainer){
Write-host "storage container '$strContainerName' already exists" -foregroundcolor yellow
return $strContainer
}
# Create new storage coantainer
try {
Write-Host "Createing storage container $strContainerName" -ForegroundColor Magenta
$strContainer = New-AzStorageContainer `
-Name $strContainerName `
-Permission Blob `
-Context $stContext
Write-host "storage container '$strContainerName' created" -foregroundcolor Green
return $strContainer
}
catch {
#failed to create resourcec group
Write-error "CDN profile '$cdnProfileName' NOT created in '$rgName'" -ErrorAction Stop
}
}
# create a CDN profile
function CreateCdnProfile{
param (
[Parameter(Mandatory=$true)][String]$name,
[Parameter(Mandatory=$true)][String]$rgName,
[Parameter(Mandatory=$true)][String]$location,
[Parameter(Mandatory=$true)][String]$env)
$cdnProfileName = "cdn-profile-$name-$env"
$cdnProfile = Get-AzCdnProfile `
-ProfileName $cdnProfileName `
-ResourceGroupName $rgName `
-ErrorAction SilentlyContinue
if($cdnProfile){
Write-host "CDN '$cdnProfileName' already exists in resource group '$rgName'" -foregroundcolor yellow
return $cdnProfileName
}
# Create a new cdn profile
try {
Write-Host "Createing cdn profile $cdnProfileName in resource group in $rgname" -ForegroundColor Magenta
$cdnProfile = New-AzCdnProfile `
-ProfileName $cdnProfileName `
-ResourceGroupName $rgName `
-Sku Standard_Microsoft `
-Location $location
Write-host "Cdn profile '$cdnProfileName' created in '$rgName'" -foregroundcolor Green
return $cdnProfileName
}
catch {
#failed to create resourcec group
Write-error "CDN profile '$cdnProfileName' NOT created in '$rgName'" -ErrorAction Stop
}
}
#create a cdn endpoint
function CreateCdnEndPoint{
param (
[Parameter(Mandatory=$true)][String]$cdnProfileName,
[Parameter(Mandatory=$true)][String]$name,
[Parameter(Mandatory=$true)][String]$rgName,
[Parameter(Mandatory=$true)][String]$location,
[Parameter(Mandatory=$true)][String]$stName,
[Parameter(Mandatory=$true)][String]$env)
$cdnEndPointName = "cdn-endpoint-$name-$env-$location"
Write-Host "-ResourceGroupName $rgName -ProfileName $cdnProfileName -Name $cdnEndPointName "
$cdnEndpoint = Get-AzCdnEndpoint `
-ResourceGroupName $rgName `
-ProfileName $cdnProfileName `
| Where-Object {$_.Name -eq $cdnEndPointName}
if($cdnEndpoint){
Write-host "cdn endpoint '$cdnEndpointName' already exists in resource group $rgName" -foregroundcolor yellow
return $cdnEndpoint
}
# Create new cdn endpoint
try {
Write-Host "Createing CDN endPoint $cdnEndPointName in resource group $rgName" -ForegroundColor Magenta
$originHostHeader = "$stName.blob.core.windows.net"
$origin = @{
Name = $originHostHeader.Replace('.','-')
HostName = $originHostHeader
};
New-AzCdnEndpoint `
-ResourceGroupName $rgName `
-ProfileName $cdnProfileName `
-OriginHostHeader $originHostHeader `
-Name $cdnEndPointName `
-Location $location `
-OptimizationType "GeneralWebDelivery" `
-Origin $origin
Write-host "cdn endpoint '$cdnEndPointName' created in '$rgName'" -foregroundcolor Green
return $cdnEndpoint
}
catch {
#failed to create cdn endpoint
Write-error "CDN endpoint '$cdnEndPointName' NOT created in '$rgName'" -ErrorAction Stop
}
}
Register-AzResourceProvider -ProviderNamespace "Microsoft.Cdn"
$rgName = CreateResourceGroup $name $location $env
$stName = CreateStorageAcccount $name $rgName $location $env
$stContainer = CreateStorageContainer $name $rgName $env $stName
$cdnProfileName = CreateCdnProfile $name $rgName $location $env
$cdnEndPoint = CreateCdnEndPoint $cdnProfileName $name $rgName $location $stName $env