Connecting to App Service’s Kudu endpoint programmatically

I recently worked on a case where a customer wanted to programmatically connect to Kudu. I’m not well versed in XML parsing (which Azure Powershell pulls for the publish profile) so I went the route of using the Azure CLI’s az rest command to pull the JSON publish profile and extracting the username and password.

$subscriptionID= "xxx"
$appname = "xx"
$RGname = "xxx"
 
$publishprofile = ((az rest --method post --url https://management.usgovcloudapi.net/subscriptions/$subscriptionID/resourceGroups/$RGname/providers/Microsoft.Web/sites/$appname/config/publishingcredentials/list?api-version=2019-08-01) | ConvertFrom-Json).properties 
 
$username = $publishprofile.publishingUsername
$password = $publishprofile.publishingPassword
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
 
$apiBaseUrl = https://$appname.scm.azurewebsites.us/api/zip/site/wwwroot/
 
Invoke-WebRequest -Uri "$apiBaseUrl" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET -OutFile "C:\users\xxx\Downloads\www.zip"