Skip to content

Instantly share code, notes, and snippets.

@michaelkc
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelkc/83679f51e6ffa34e9ef8 to your computer and use it in GitHub Desktop.
Save michaelkc/83679f51e6ffa34e9ef8 to your computer and use it in GitHub Desktop.
Creating web hosting plan and ensuring websites and staging slots all use it
Import-Module Azure
Set-StrictMode -version 4
$location = "North Europe"
$resourceGroupName = 'Default-Web-' + $location.Replace(' ', '');
$webHostingPlanName = "StandardSmall";
$websiteResourceType = "Microsoft.Web/sites";
$stagingSlotResourceType = "Microsoft.Web/sites/slots";
function New-AzureWebHostingPlan($hostingPlanName)
{
Switch-AzureMode -Name AzureResourceManager -Verbose:$false
#create a new web hosting plan - Small Standard machine
$p=@{"name"= $hostingPlanName;"sku"= "Standard";"workerSize"= "0";"numberOfWorkers"= 1}
New-AzureResource -ApiVersion 2014-04-01 -Name $hostingPlanName -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/serverFarms -Location $location -PropertyObject $p -Verbose -Force
Switch-AzureMode -Name AzureServiceManagement -Verbose:$false
}
function Update-AzureWebSiteWebHostingPlan($resource, $webHostingPlanName, $resourceType = $websiteResourceType, $parentResource = $null)
{
Switch-AzureMode -Name AzureResourceManager -Verbose:$false
Set-AzureResource -Name $resource -ResourceGroupName $resourceGroupName -ParentResource $parentResource -ResourceType $resourceType -ApiVersion '2014-04-01' -PropertyObject @{ 'ServerFarm' = $webHostingPlanName }
Switch-AzureMode -Name AzureServiceManagement -Verbose:$false
}
function Get-AzureResourceProperties($name, $resourceType = $websiteResourceType, $parentResource = $null)
{
Switch-AzureMode -Name AzureResourceManager -Verbose:$false
$currentWebHostingPlanName = (Get-AzureResource -Name $name -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ParentResource $parentResource -ApiVersion '2014-04-01').Properties
Switch-AzureMode -Name AzureServiceManagement -Verbose:$false
return $currentWebHostingPlanName
}
function Get-AzureWebsiteResource()
{
Switch-AzureMode -Name AzureResourceManager -Verbose:$false
return Get-AzureResource | Where-Object { $_.ResourceType -eq $websiteResourceType -and $_.ParentResource -eq $null}
Switch-AzureMode -Name AzureServiceManagement -Verbose:$false
}
function Get-AzureWebsiteStagingResource()
{
Switch-AzureMode -Name AzureResourceManager -Verbose:$false
return Get-AzureResource | Where-Object { $_.ResourceType -eq $stagingSlotResourceType}
Switch-AzureMode -Name AzureServiceManagement -Verbose:$false
}
Switch-AzureMode -Name AzureServiceManagement -Verbose:$false
$standardSmallWHP = Get-AzureWebHostingPlan | where {$_.Name -eq $webHostingPlanName}
if (-not $standardSmallWHP)
{
"Adding StandardSmall WebHostingPlan"
New-AzureWebHostingPlan $webHostingPlanName
$standardSmallWHP = Get-AzureWebHostingPlan | where {$_.Name -eq $webHostingPlanName}
}
# This filters out staging slots, which cannot have their web hosting plan set independently from their parent
$existingWebsites = Get-AzureWebsiteResource
foreach ($site in $existingWebsites)
{
$name = $site.Name
"Checking website $name"
$currentWebHostingPlanName = (Get-AzureResourceProperties $name).serverFarm
if ($currentWebHostingPlanName -ne $webHostingPlanName)
{
"Switching web hosting plan for $name"
Update-AzureWebSiteWebHostingPlan $name $webHostingPlanName
}
}
$stagingSlots = Get-AzureWebsiteStagingResource
foreach ($stagingSite in $stagingSlots)
{
$name = $stagingSite.Name
"Checking staging slot $name"
$currentWebHostingPlanName = (Get-AzureResourceProperties $name $stagingSlotResourceType $stagingSite.ParentResource).serverFarm
if ($currentWebHostingPlanName -ne $webHostingPlanName)
{
"Switching web hosting plan for $name"
Update-AzureWebSiteWebHostingPlan $name $webHostingPlanName $stagingSlotResourceType $stagingSite.ParentResource
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment