如何使用PowerShell部署到Azure App Service?

时间:2017-08-23 21:59:02

标签: powershell azure azure-web-sites

我已经浏览过PowerShell中Azure和AzureRM命令行开关中的数千个命令,我仍然不确定如何执行此操作。

到目前为止我的工作:

  • 安装Azure和AzureRM模块并将其导入脚本
  • 生成" *。publishsettings"来自get-AzurePublishSettingsFile命令的文件
  • 导入" *。publishsettings"文件
  • 可以通过" Stop-AzureWebsite"访问该网站。和#34; Start-AzureWebsite"命令行开关

我需要做什么:

  • 创建新部署并将文件推送到应用服务站点。

注意:我没有Visual Studio项目和.csproj文件配置。我只想获取文件夹的内容并将其推送到网站。

任何帮助都会很有用,因为文档在细节上非常糟糕,并且PowerShell中有成千上万的命令要经过。

3 个答案:

答案 0 :(得分:5)

您可以查看blog:Deploy an App Service using Azure PowerShell to a Deployment Slot

Get-AzurePublishSettingsFile
Import-AzurePublishSettingsFile .\Your-Publish-Settings-credentials.publishsettings
Get-AzureSubscription
Select-AzureSubscription -SubscriptionName "The Subscription Name containing the slot"
Set-AzureSubscription -SubscriptionId "ID of subscription"

$WebAppName = "standard(staging)"
Get-AzureWebsite -Name $WebAppName
Publish-AzureWebsiteProject -Name $WebAppName -Package "C:\PowerShell\standard.zip" -Slot "staging"

答案 1 :(得分:2)

答案 2 :(得分:0)

不幸的是,被接受的答案给了我以下错误:

  

Get-AzureWebSite:找不到请求的值'PremiumV2'

StackOverflow answer建议改用Get-AzureRmWebApp,但这给身份验证带来了一些挑战。经过一番搜索,我发现以下article准确地解释了我所需要的:一种无需人工干预即可发布到Azure的方法。

请在下面查看脚本的非常简化的版本。

#In the Azure portal go to (search for) "Azure Active Directory" -> 
#"Properties" -> Directory ID
$TenantId = "<Azure Active Directory Id>"

#In the Azure portal go to (search for) "Subscriptions" -> Subscription ID
$SubscriptionId = "<Azure Subscription Id>"

#In the Azure portal go to (search for) "Azure Active Directory" -> "App registrations" -> 
#Create a new registration, this will give you the ID and Secret below.
#Make sure to give your new app registration sufficient rights to your app service 
$ServicePrincipleApplicationId = "<Service Principle Id>"
$ServicePrincipleApplicationSecret = "<Service Principle Secret>"

$WebAppPath = "<Local folder where your package is located>"
$ResourceGroupName = "<The name of the Azure resource group that contains your app service>"
$WebAppName = "<The name of your Azure app service>"
$WebAppSlot = "<The name of the deployment slot you want to publish to>"

$MSDeployPath = "C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe"
$source = "-source:contentPath=$WebAppPath"
$publishProfileOutputPath = Join-Path -Path $ENV:Temp -ChildPath 'publishprofile.xml'
$dest = "-dest:contentPath=d:\home\site\wwwroot\,publishSettings=$publishProfileOutputPath"
$SecurePassword = $ServicePrincipleApplicationSecret | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ServicePrincipleApplicationId, $securePassword
$connectParameters = @{
    Credential     = $Credential
    TenantId       = $TenantId
    SubscriptionId = $SubscriptionId
}

Add-AzureRmAccount @connectParameters -ServicePrincipal

Get-AzureRmWebAppSlotPublishingProfile -OutputFile $publishProfileOutputPath -Format WebDeploy -ResourceGroupName $ResourceGroupName -Name $WebAppName -Slot $WebAppSlot

Stop-AzureRmWebAppSlot -ResourceGroupName $ResourceGroupName -Name $WebAppName -Slot $WebAppSlot

& $MSDeployPath @('-verb:sync', $source, $dest)

Start-AzureRmWebAppSlot -ResourceGroupName $ResourceGroupName -Name $WebAppName -Slot $WebAppSlot