Created
May 26, 2016 20:49
-
-
Save DamianStanger/cbe83af0f6031780cac2ae56be072406 to your computer and use it in GitHub Desktop.
A powershell script that will enable http logging on an azure app service. logging will go to blob storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Version 3.0 | |
#Requires -Module AzureRM.Resources | |
#Requires -Module Azure.Storage | |
Param( | |
[string] $Environment = 'dev', | |
[string] $Location = 'uk', | |
[string] $Company = 'moocowco', | |
[string] $WebLogsStorageAccountKey = 'mystorageaccountkey' | |
) | |
$Environment = $Environment.ToLower() | |
$Location = $Location.ToLower() | |
$Company = $Company.ToLower() | |
Set-StrictMode -Version 3 | |
Import-Module Azure | |
Write-Host -ForegroundColor DarkCyan '------------ Parameters --------------' | |
Write-Host -ForegroundColor DarkCyan '$Environment :' $Environment | |
Write-Host -ForegroundColor DarkCyan '$Location :' $Location | |
Write-Host -ForegroundColor DarkCyan '$Company :' $Company | |
Write-Host -ForegroundColor DarkCyan '$WebLogsStorageAccountKey :' $WebLogsStorageAccountKey | |
Write-Host | |
function EnableWebSiteDiagnosticsOnApis([string[]]$apps, [string]$serviceName){ | |
Write-Host -ForegroundColor Cyan "EnableWebSiteDiagnosticsOnApis processing '$apps' : In service '$serviceName'" | |
foreach($app in $apps){ | |
EnableWebSiteDiagnostics $app $serviceName | |
} | |
} | |
function EnableWebSiteDiagnostics([string]$webAppName, [string]$serviceName){ | |
$fullWebAppName = "$webAppName-$Environment-$Location-$Company" | |
Write-Host -ForegroundColor Cyan "EnableWebSiteDiagnostics for '$webAppName' : '$fullWebAppName' within the service '$serviceName'" | |
$weblogsAccount = "logs$Environment$Location$Company" | |
$storagecontext = New-AzureStorageContext -StorageAccountName $weblogsAccount -StorageAccountKey $WebLogsStorageAccountKey -ErrorAction Stop | |
$container = Get-AzureStorageContainer -Context $storagecontext -Name $webAppName | |
if($container){ | |
Write-Host -ForegroundColor Cyan " Container '$webAppName' in storageAccount '$weblogsAccount' exists." | |
} | |
else{ | |
Write-Host -ForegroundColor Cyan " Creating container '$webAppName' in storageAccount '$weblogsAccount'" | |
$container = New-AzureStorageContainer -Name $webAppName -Permission Container -Context $storageContext -Verbose -ErrorAction Stop | |
} | |
$resourceGroupName = "$serviceName-$Environment-$Location-$Company" | |
$resourceConfig = Get-AzureRmResource -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config -ResourceName "$fullWebAppName/logs" -ApiVersion 2015-08-01 -ErrorAction Stop | |
if($resourceConfig.Properties.HttpLogs.AzureBlobStorage.Enabled){ | |
Write-Host -ForegroundColor Cyan " WEBSITE_HTTPLOGGING_CONTAINER_URL already has the value:" $resourceConfig.Properties.HttpLogs.AzureBlobStorage.SasUrl | |
} | |
else { | |
$startTime = Get-Date -Date '2000-01-01T00:00:00' | |
$expiryTime = Get-Date -Date '2100-01-01T00:00:00' | |
$sasToken = New-AzureStorageContainerSASToken -StartTime $startTime -ExpiryTime $expiryTime -Context $storagecontext -Name $webAppName -Permission rwdl -Verbose -ErrorAction Stop | |
$sasToken = $container.CloudBlobContainer.Uri.AbsoluteUri + $sasToken.ToString() | |
Write-Host -ForegroundColor Cyan " Set-AzureRmResource -ResourceName '$fullWebAppName/logs' with sasToken '$sasToken' -startTime '$startTime' -expiryTime '$expiryTime'" | |
#$appSettingsHash = @{"WEBSITE_HTTPLOGGING_CONTAINER_URL" = $sasToken} | |
$PropertiesObject = @{ | |
httpLogs = @{ | |
azureBlobStorage = @{ | |
sasUrl = $sasToken; | |
enabled = $true | |
} | |
} | |
} | |
#Set-AzureWebsite -Name $fullWebAppName -Slot production -HttpLoggingEnabled $true -AppSettings $appSettingsHash -Verbose -ErrorAction Stop | |
Set-AzureRmResource -ResourceName "$fullWebAppName/logs" -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config -Properties $PropertiesObject -ApiVersion 2015-08-01 -Force -ErrorAction Stop | |
Write-Host -ForegroundColor Cyan " Set-AzureRmResource -ResourceName $fullWebAppName/logs : complete" | |
} | |
Write-Host -ForegroundColor Cyan " EnableWebSiteDiagnostics Fin!" | |
} | |
# Its assumed the following 2 app services related storage account and resource group exists, this script will not create them | |
# The names of the resourcess conform to a convention that allows for a flexability in scripting | |
# All resources group names are of the form ServiceName-Environment-Location-Company | |
# All app service names are of the form App-Environment-Location-Company | |
# All storage accounts are in the format logEnvironmentLocationCompany | |
$serviceName = "food" | |
$apps = @("foo", "bar") | |
EnableWebSiteDiagnosticsOnApis $apps $serviceName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment