使用PowerShell而不通过REST进行Azure Function App部署?

时间:2018-11-20 20:34:28

标签: powershell azure-functions azure-powershell

我目前有一个bash脚本,该脚本使用dotnet编译Function App,并使用Azure CLI推送部署.zip文件。该脚本本质上是:

dotnet clean --configuration Release
dotnet build --configuration Release
cd bin/Release/netstandard2.0
zip -r ${functionappName}.zip *
az functionapp deployment source config-zip -g group -n functionapp --src ${functionappName}.zip

在此之前,使用有权部署到功能应用程序的服务主体来完成az login

我想将其翻译成PowerShell。我可以进行dotnet编译和zip文件的创建,但是还无法确定部署。我尝试使用:

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password )))
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method POST -InFile $filePath -ContentType "multipart/form-data"

...,其中usernamepassword是服务主体ID和机密,但这会导致401 - Unauthorized: Access is denied due to invalid credentials错误。

是否可以使用PowerShell通过服务主体将zip部署到功能应用程序?

1 个答案:

答案 0 :(得分:2)

如果使用PowerShell,则可以在部署期间直接获取发布凭据。

Login-AzureRmAccount
Get-AzureRmSubscription | Select SubscriptionName, SubscriptionId
Select-AzureRmSubscription -SubscriptionName "My Subscription"
$resourceGroup = "MyResourceGroup"
$functionAppName = "MyFunctionApp"

获得部署凭据

$creds = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroup -ResourceType Microsoft.Web/sites/config ` -ResourceName $functionAppName/publishingcredentials -Action list -ApiVersion 2015-08-01 -Force
$username = $creds.Properties.PublishingUserName
$password = $creds.Properties.PublishingPassword
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

现在开始部署

$apiUrl = "https://$functionAppName.scm.azurewebsites.net/api/zip/site/wwwroot"
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method PUT -InFile $zipFilePath -ContentType "multipart/form-data"