繁体   English   中英

如何使用 PowerShell(az 模块)部署到 Azure 应用服务?

[英]How do I deploy to Azure App Service with PowerShell (az module)?

我想从 Powershell 部署应用服务。 我只想使用发布配置文件(天蓝色帐户没有密码)。

我尝试过FTP服务,但有时文件会被正在运行的用户阻止。 我想我必须停止应用服务。

有powershell命令,如:

Publish-AzWebApp

但是首先我需要登录:

Connect-AzAccount

并传递我想要避免的凭据。

有什么方法可以仅根据发布配置文件(无帐户登录)调用Publish-AzWebApp

Connect-AzAccount有其他登录选项(令牌或证书)。 不幸的是,我不知道如何生成它。

顺便说一句,有一个关于它的主题: 如何使用 PowerShell 部署到 Azure 应用服务? 但它很旧,现在推荐使用模块“az”。

有什么方法可以仅基于发布配置文件来调用Publish-AzWebApp(不通过帐户登录)?

不,你不能。 如果要使用Publish-AzWebApp ,则始终需要使用Connect-AzAccount登录,无论使用什么参数,请Connect-AzAccount 此处的示例。

如果您要使用Powershell仅基于发布配置文件来部署Web应用,则解决方法是通过powershell使用Kudu API

$username = "`$webappname"
$password = "xxxxxxx"
# Note that the $username here should look like `SomeUserName`, and **not** `SomeSite\SomeUserName`
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))
$userAgent = "powershell/1.0"

$apiUrl = "https://joywebapp.scm.azurewebsites.net/api/zipdeploy"
$filePath = "C:\Users\joyw\Desktop\testdep.zip"
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST -InFile $filePath -ContentType "multipart/form-data"

以下是使用新推荐的Az 模块通过 PowerShell (PS) 部署和发布 Azure 应用服务的分步过程。 您必须在运行脚本之前安装Azure CLI

$subscription = 'Visual Studio Enterprise – MPN' #Change it as per your Azure subscription
$location = 'Central US' #Change it according to the location where you want to host your web app
$resourceGroup = 'MyWebAppResourceGroup'

$ErrorActionPreference = "Stop" #this ensures that script stops executing at first error in the script.


Write-host "Installing nuget package provider"
Install-PackageProvider -Name NuGet -Scope CurrentUser -MinimumVersion 2.8.5.201 -Force
Write-Host "Setting Power Shell gallery"
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted

Write-Host "Installing missing PowerShell modules required for running this script.."
if ((Get-InstalledModule -Name "Az.Accounts" -ErrorAction SilentlyContinue) -eq $null) {
    Write-Host "Az.Accounts module missing. Now installing..."
    Install-Module -Name Az.Accounts -Scope CurrentUser
}


if ((Get-InstalledModule -Name "Az.Websites" -ErrorAction SilentlyContinue) -eq $null) {
    Write-Host "Az.Websites module missing. Now installing .."
    Install-Module -Name Az.Websites -Scope CurrentUser
}
Write-Host "Installing PowerShell modules completed."

Write-Host "Staring to import PowerShell modules in current session...."
Import-Module Az.websites
Write-Host "Importing PowerShell modules completed."


#This is required due to an issue due to which PowerShell fails to connect with online resources. This issue is machine specific. So you can comment it if not required.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Write-Host "Connecting to Azure account..."
Connect-AzAccount 

Write-Host "Setting subscription for current session..."
az account set --subscription $subscription

Write-Host "Creating resource group..."
az group create --name $resourceGroup --location $location

$webAppName="WelcomeCloudApp"  
$appServicePlan="WelcomeCloudAppServicePlan"  

Write-Host "Creating WebApp Service plan..."
New-AzAppServicePlan  -Name $appServicePlan -ResourceGroupName $resourceGroup  -Location $location -Tier 'Free' #-Debug  

Write-Host "Creating WebApp..."
New-AzWebApp -Name $webAppName -Location $location -AppServicePlan $appServicePlan -ResourceGroupName $resourceGroup 

Write-Host "Publishing WebApp..."
Publish-AzWebApp -ResourceGroupName $resourceGroup -Name $webAppName -ArchivePath WelcomeCloudAppService.zip  

Write-Host "Finished installing your web app. Bye!"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM