通过 Powershell 从 KUDU (Azure Web App) 下载文件

Download files from KUDU (Azure Web App) Via Powershell

我想通过 Powershell 从 Kudu 下载文件,使用 Invoke-WebRequest 我得到的只是 filename.log 没有日志数据,从我在日志文件中看到的,它是登录屏幕关闭天蓝色 "Sign in to your account".

URL

Invoke-WebRequest“https://AppName.scm.azurewebsites.net/api/vfs/LogFiles/FileName.Log”-OutFile $FilePath1 获取 ChildItem -File $FilePath1 -Recurse |设置 AzureStorageBlobContent -Container FilesContainer -Context $StorageContext

需要在 Invoke-WebRequest 的 Headers 中提供您的 webapp 的 Publish Profile 的用户名和密码以进行身份​​验证。

您可以在发布配置文件中获取用户名和密码。您可以从 Azure Web App 下载发布配置文件。并在 publishProfile 部分引用 userName 和 userPWD 值。

# User name from WebDeploy Publish Profile. Use backtick while assigning variable content  
$userName = "{userName}"  
# Password from WebDeploy Publish Profile  
$password = "{Password}"  
# Encode username and password to base64 string  
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $userName, $password)))

 # pass the authentication to Header
Invoke-WebRequest -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET -OutFile $filePath -ContentType "multipart/form-data"

您也可以通过脚本获取用户名和密码,见下例:

$ResGroupName = ""
$WebAppName = ""
$LogFolder = ""

# Get publishing profile for web application
$WebApp = Get-AzWebApp -Name $WebAppName -ResourceGroupName $ResGroupName
[xml]$publishingProfile = Get-AzWebAppPublishingProfile -WebApp $WebApp

# Create Base64 authorization header
$username = $publishingProfile.publishData.publishProfile[0].userName
$password = $publishingProfile.publishData.publishProfile[0].userPWD
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

# pass the authentication to Header
Invoke-WebRequest -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET -OutFile $filePath -ContentType "multipart/form-data"