Powershell中带有资源管理器的Azure发布Web应用 [英] Azure Publish Web App with Resource Manager in Powershell

查看:65
本文介绍了Powershell中带有资源管理器的Azure发布Web应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Azure上的应用程序服务上发布Web应用程序包.我需要创建一个Powershell脚本以从Cloud Shell运行,以便发布该程序包.我写了下面的代码

I am trying to publish a Web App package on an App Service on Azure. I need to crate a Powershell script to run from the Cloud Shell, in order to publish the package. I wrote the following code

Import-AzurePublishSettingsFile - $WebApp.PublishProfilePath
Publish-AzureWebsiteProject -Package $WebApp.ZipPath -Name $WebApp.WebAppName

此代码可在我的本地计算机上运行,​​但不能在出现以下错误的Cloud Shell中使用:

This code works on my local machine, but not in the Cloud Shell where I get the following errors:

Import-AzurePublishSettingsFile : The term 'Import-AzurePublishSettingsFile' 
is not recognized as the name of a cmdlet, function, script file, or 
operable program.

Publish-AzureWebsiteProject : The term 'Publish-AzureWebsiteProject' is not 
recognized as the name of a cmdlet, function, script file, or operable 
program.

我猜这些错误是由于这些cmdlet来自旧的Classic Manager(在Cloud Shell中不可用)造成的.

I guess these errors are caused by the fact that these cmdlets comes from the old Classic Manager, which is not available in the Cloud Shell.

基本上,我需要使用脚本从Cloud Shell发布 Web应用程序包.我该如何实现?我还有其他选择吗?

Basically I need to publish a Web App package from the Cloud Shell with a script. How can I achieve that? Do I have other options?

推荐答案

从Hannel建议的文档链接开始,我终于通过

Starting from the documentation link suggested by Hannel, I finally solved by invoking REST API with Powershell. With Azure CLI is much easier, but I have a more complex script already written with Powershell.

最终代码是这样的:

# Getting credentials
$creds = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName `
    -ResourceType Microsoft.Web/sites/config `
    -ResourceName "$($WebApp.WebAppName)/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)))

# Deploy Web App
Invoke-RestMethod -Uri "https://$($WebApp.WebAppName).scm.azurewebsites.net/api/zipdeploy" `
    -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)} `
    -Method POST `
    -InFile $WebApp.ZipPath `
    -ContentType "multipart/form-data"  `
    -UseBasicParsing `
    -ErrorAction Stop | Out-Null

这篇关于Powershell中带有资源管理器的Azure发布Web应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆