Using powershell, I am trying to get the azure webapp log files and copy them to azure storage account container and then delete
$webAppName = "WA01"
$resourceGroupName = "RG01"
$storageAccountName = "SA01"
$containerName = "container01"
$numberOfDays = 90
# Generate Kudu API URL for log files
$kuduApiUrl = "https://${webAppName}.scm.azurewebsites.net/api/vfs/LogFiles/"
# Retrieve publishing profile for the Azure Web App
Write-Host "Retrieving publishing profile for $webAppName..."
$publishingProfile = Get-AzWebAppPublishingProfile -ResourceGroupName $resourceGroupName -Name $webAppName
# Parse publishing profile XML to get credentials
[xml]$publishingProfileXml = $publishingProfile
$publishingUserName = $publishingProfileXml.publishData.publishProfile[0].userName
$publishingPassword = $publishingProfileXml.publishData.publishProfile[0].userPWD
# Convert credentials to base64
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("${publishingUserName}:${publishingPassword}"))
Write-Host "Retrieving log files from $webAppName..."
$logFilesToBackup = (Invoke-RestMethod -Uri $kuduApiUrl -Headers @{Authorization=("Basic $base64AuthInfo")} -Method GET).Where({
$mtimeValue = $_.mtime.Split('.')[0] # Remove milliseconds
$mtimeValue = $mtimeValue.Substring(0, 19) # Remove time zone offset
[DateTime]::ParseExact($mtimeValue, "yyyy-MM-dd'T'HH:mm:ss", [System.Globalization.CultureInfo]::InvariantCulture) -lt $cutoffDate
})
# Perform backup actions...
Write-Host "Performing backup actions for $webAppName..."
foreach ($logFile in $logFilesToBackup) {
$logFileName = $logFile.name
$logFileUrl = $logFile.href
Write-Host "Backing up log file: $logFileName"
$blobName = "$webAppName/$($logFileUrl.Substring($logFileUrl.LastIndexOf("/") + 1))"
Start-AzStorageBlobCopy -SrcUri $logFileUrl -DestContainer $containerName -DestBlob $blobName -Context $storageAccountContext
}
# Perform deletion actions...
Write-Host "Deleting log files older than $numberOfDays days from $webAppName..."
foreach ($logFile in $logFilesToBackup) {
$logFileName = $logFile.name
Write-Host "Deleting log file: $logFileName"
Invoke-RestMethod -Uri $logFileUrl -Headers @{Authorization=("Basic $base64AuthInfo")} -Method DELETE
}
- Here is the warning message for copying to blob (nothing is copied)
WARNING: Ignore mismatch source storage context.. The source uri is https://wa01.scm.azurewebsites.net/api/vfs/LogFiles/kudu/, the end point is https://sa01.blob.core.windows.net/.
- Here is the error for the delete action (need to force the delete activity)
Invoke-RestMethod : {"Message":"Cannot delete directory. It is either not empty or access is not allowed."}
The intention is to copy files and directories to storage account container and after copy delete from webapp



