Reputation: 3558
As part of our CI process, I'd like to clear out all of the log files on our App Service before deploying new code to it. I believe the best way to do this is with a KUDU API call using a PowerShell script. I believe that I need to pass it a command. So this is what I've come up with so far based on some other similar questions asked. However, when I run this, I get a "Error 403 - This web app is stopped." response back. So I'm doing something wrong.
The important part is the "$kudoApiCommand".
param(
[string]$resourceGroupName,
[string]$webAppName,
[string]$slotName=""
)
function Get-AzureRmWebAppPublishingCredentials($resourceGroupName, $webAppName, $slotName = $null){
if ([string]::IsNullOrWhiteSpace($slotName)){
$resourceType = "Microsoft.Web/sites/config"
$resourceName = "$webAppName/publishingcredentials"
}
else{
$resourceType = "Microsoft.Web/sites/slots/config"
$resourceName = "$webAppName/$slotName/publishingcredentials"
}
$publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName
$resourceGroupName -ResourceType $resourceType -ResourceName
$resourceName -Action list -ApiVersion 2015-08-01 -Force
Write-Host $publishingCredentials
return $publishingCredentials
}
function Get-KuduApiAuthorisationHeaderValue($resourceGroupName,$webAppName, $slotName = $null){
$publishingCredentials = Get-AzureRmWebAppPublishingCredentials $resourceGroupName $webAppName $slotName
Write-Host $publishingCredentials.Properties.PublishingUserName
Write-Host $publishingCredentials.Properties.PublishingPassword
return ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
}
function Delete-WebAppLogFiles($resourceGroupName, $webAppName, $slotName = ""){
$kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValue
$resourceGroupName $webAppName $slotName
if ($slotName -eq ""){
$kuduApiUrl = "https://$webAppName.scm.azurewebsites.net/api/command"
}
else{
$kuduApiUrl = "https://$webAppName`-$slotName.scm.azurewebsites.net/api/command"
}
$kudoApiCommand = @{
command='del * /S /Q'
dir='d:\\home\\LogFiles'
}
Write-Output $kuduApiUrl
Write-Output $kuduApiAuthorisationToken
Invoke-RestMethod -Uri $kuduApiUrl `
-Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} `
-Method POST
-Body $kudoApiCommand
}
Delete-WebAppLogFiles $resourceGroupName $webAppName $slotName
Upvotes: 2
Views: 1847
Reputation: 3558
Got it. This is what worked for me:
param(
[string]$resourceGroupName,
[string]$webAppName,
[string]$slotName=""
)
function Get-AzureRmWebAppPublishingCredentials($resourceGroupName, $webAppName, $slotName = $null){
if ([string]::IsNullOrWhiteSpace($slotName)){
$resourceType = "Microsoft.Web/sites/config"
$resourceName = "$webAppName/publishingcredentials"
}
else{
$resourceType = "Microsoft.Web/sites/slots/config"
$resourceName = "$webAppName/$slotName/publishingcredentials"
}
$publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force
Write-Host $publishingCredentials
return $publishingCredentials
}
function Get-KuduApiAuthorizationHeaderValue($resourceGroupName, $webAppName, $slotName = $null){
$publishingCredentials = Get-AzureRmWebAppPublishingCredentials $resourceGroupName $webAppName $slotName
Write-Host $publishingCredentials.Properties.PublishingUserName
Write-Host $publishingCredentials.Properties.PublishingPassword
return ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
}
function Delete-WebAppLogFiles($resourceGroupName, $webAppName, $slotName = ""){
$apiAuthorizationToken = Get-KuduApiAuthorizationHeaderValue $resourceGroupName $webAppName $slotName
if ($slotName -eq ""){
$apiUrl = "https://$webAppName.scm.azurewebsites.net/api/command"
}
else{
$apiUrl = "https://$webAppName`-$slotName.scm.azurewebsites.net/api/command"
}
$apiCommand = @{
#command='del *.* /S /Q /F'
command = 'powershell.exe -command "Remove-Item -path d:\\home\\LogFiles\\* -recurse"'
dir='d:\\home\\LogFiles'
}
Write-Output $apiUrl
Write-Output $apiAuthorizationToken
Write-Output $apiCommand
Invoke-RestMethod -Uri $apiUrl -Headers @{"Authorization"=$apiAuthorizationToken;"If-Match"="*"} -Method POST -ContentType "application/json" -Body (ConvertTo-Json $apiCommand)
}
Delete-WebAppLogFiles $resourceGroupName $webAppName $slotName
Upvotes: 2