В рамках нашего процесса CI я хотел бы очистить все файлы журналов в нашей службе приложений, прежде чем развертывать в ней новый код. Я считаю, что лучший способ сделать это — вызвать API KUDU с помощью сценария PowerShell. Я считаю, что мне нужно передать ему команду. Итак, это то, что я придумал до сих пор, основываясь на некоторых других подобных вопросах. Однако, когда я запускаю это, я получаю «Ошибка 403 — это веб-приложение остановлено». ответ назад. Значит, я что-то делаю не так.
Важная часть — «$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