作为CI流程的一部分,我想在部署新代码之前清除App Service上的所有日志文件。我相信最好的方法是使用PowerShell脚本进行KUDU API调用。我相信我需要传递一个命令。所以这是我到目前为止基于其他一些类似问题提出的问题。但是,当我运行此操作时,我得到一个错误403 - 此Web应用程序已停止。"回复。所以我做错了。
重要的部分是" $ 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
答案 0 :(得分:2)
知道了。这对我有用:
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