通过Powershell创建标准定价层的Azure网站

时间:2015-05-21 12:02:25

标签: powershell azure azure-web-sites azure-app-service-plans

我正在尝试修复我的持续部署方案,为此,Azure网站必须存在,并且能够在分段和生产之间进行切换。我们希望此网站在Standard定价层中工作。

我目前的脚本创建了一个新的ResourceGroup,托管计划,并在创建之后,创建了网站本身。我面临的问题是网站始终处于Free模式。我应该能够通过使用Set-AzureResource cmdlet来解决这个问题,但是这个问题告诉我应该指定Location。问题是这个特定的cmdlet没有Location参数。

Set-AzureResource : {
  "Error": {
    "Code": "LocationRequired",
    "Message": "The location property is required for this definition.",
    "Target": null,
    "Details": null
  }
}

这是我用来创建所有资源的(简化)脚本:

#Create the resource group
New-AzureResourceGroup -Location $location -Name $resourceGroupName -Force

#Create the hosting plan
$hostingPlanParameters = @{"name" = $hostingPlanName; "sku" = "Standard"; "computeMode" = "Standard"; "workerSize" = "0"; "numberOfWorkers" = "1"}

New-AzureResource -ApiVersion 2014-04-01 -Name $hostingPlanName -ResourceGroupName $resourceGroupName `
                            -ResourceType Microsoft.Web/serverFarms -Location $location `
                            -PropertyObject $hostingPlanParameters -Verbose -Force
#Create the website
$analyticsSite = @{"sku" = "Standard"; "webHostingPlan" = $hostingplan; "computeMode" = "Standard"; }
New-AzureResource  -Name $label -ResourceType Microsoft.Web/sites `
                             -ResourceGroupName $resourceGroupName -Location $location `
                             -ApiVersion $apiVersion -PropertyObject $analyticsSite -Force

Set-AzureResource -Name $label -ResourceType Microsoft.Web/sites `
                             -ResourceGroupName $resourceGroupName -ApiVersion $apiVersion `
                             -PropertyObject $analyticsSite -Force

我读过网站应该继承指定主机方案的sku,所以我不需要更新它。这似乎不适用于我的上述脚本。指定了托管计划,但未继承设置。

创建的主机方案属性如下所示:

PropertiesText    : {
                      "name": "devHostingPlanWestEU10",
                      "sku": "Standard",
                      "workerSize": 0,
                      "workerSizeId": 0,
                      "numberOfWorkers": 1,
                      "currentWorkerSize": 0,
                      "currentWorkerSizeId": 0,
                      "currentNumberOfWorkers": 1,
                      "status": 0,
                      "webSpace": "ResourceGroupWestEU10-WestEuropewebspace",
                      "subscription": "ad7add9b-8b7a-45df-8e95-0e7fccbr78a5",
                      "adminSiteName": null,
                      "hostingEnvironment": null,
                      "maximumNumberOfWorkers": 0,
                      "planName": null,
                      "perSiteScaling": null,
                      "hostingEnvironmentId": null
                    }

这对我来说很好。 创建网站后,将打印这些属性:

PropertiesText    : {
                      "name": "Testert10",
                      "state": "Running",
                      "hostNames": [
                        "testert10.azurewebsites.net"
                      ],
                      "webSpace": "ResourceGroupWestEU10-WestEuropewebspace",
                      ...
                      "repositorySiteName": "Testert10",
                      "owner": null,
                      "usageState": 0,
                      "enabled": true,
                      ...
                      "computeMode": null,
                      "serverFarm": "Default1",
                      "serverFarmId": null,
                      "lastModifiedTimeUtc": "2015-05-21T11:52:30.773",
                      "storageRecoveryDefaultState": "Running",
                      "contentAvailabilityState": 0,
                      "runtimeAvailabilityState": 0,
                      "siteConfig": null,
                      "deploymentId": "Testert10",
                      "trafficManagerHostNames": null,
                      "sku": "Free",
                      "premiumAppDeployed": null,
                      "scmSiteAlsoStopped": false,
                      "targetSwapSlot": null,
                      "hostingEnvironment": null,
                      "microService": "WebSites",
                      "gatewaySiteName": null,
                      "kind": null,
                      "cloningInfo": null,
                      "hostingEnvironmentId": null
                    }

如您所见,computeMode,serverFarm,hostingEnvironment和sku未使用我在$analyticsSite对象中设置的属性进行设置。

因此我可能需要更新资源,但这会引发上述错误。

我也尝试使用New-AzureWebsite,以Troy Hunt's blogpost为例。但是,这篇文章还依赖于使用Set-AzureResource,所以我会陷入同样的​​问题。此示例的另一个问题是您无法控制创建网站的资源组和托管计划,这会在搜索网站时造成一些麻烦。

3 个答案:

答案 0 :(得分:5)

在新的RM cmdlet中可以轻松实现。确保首先拥有最新版本的Azure PowerShell。

首先创建一个定义标准价格层的应用服务计划,然后使用应用服务计划创建一个Web应用。

function Create-AppServicePlan()
{
    #https://msdn.microsoft.com/en-us/library/mt619306.aspx
    $resource = Find-AzureRmResource -ResourceNameContains $ServicePlanName -ResourceGroupNameContains $ResourceGroupName -ResourceType "Microsoft.Web/ServerFarms"
    if(!$resource)
    {
        # Specify the Tier type that you would like
        $servicePlan = New-AzureRmAppServicePlan -ResourceGroupName $ResourceGroupName -Name $ServicePlanName -Location $WebAppLocation -Tier Standard -NumberofWorkers 1 -WorkerSize "Small"
    }
}

接下来使用应用服务计划作为参数创建Web应用。

function Create-AzureRmWebApp()
{
    #https://msdn.microsoft.com/en-us/library/mt619250.aspx
    $resource = Find-AzureRmResource -ResourceNameContains $WebAppName -ResourceGroupNameContains $ResourceGroupName -ResourceType "Microsoft.Web/sites"
    if(!$resource)
    {
        $webApp = New-AzureRmWebApp -ResourceGroupName $ResourceGroupName -Name $WebAppName -Location $WebAppLocation -AppServicePlan $ServicePlanName
    }
}

这是经过验证的完整工作脚本。

$ServicePlanName = "PSScriptAppServicePlann"
$WebAppName = "WebAppByPSlooksCool"
$ResourceGroupName = "MyResourceGroup"
$WebAppLocation = "australiaeast"
$ErrorActionPreference = "Stop"

# Step 1: Create the application service plan
Create-AppServicePlan

# Step 2: Create the web app using the service plan name.
Create-AzureRmWebApp

function Create-AzureRmWebApp()
{
    #https://msdn.microsoft.com/en-us/library/mt619250.aspx
    $resource = Find-AzureRmResource -ResourceNameContains $WebAppName -ResourceGroupNameContains $ResourceGroupName -ResourceType "Microsoft.Web/sites"
    if(!$resource)
    {
        $webApp = New-AzureRmWebApp -ResourceGroupName $ResourceGroupName -Name $WebAppName -Location $WebAppLocation -AppServicePlan $ServicePlanName
    }
}

function Create-AppServicePlan()
{
    #https://msdn.microsoft.com/en-us/library/mt619306.aspx
    $resource = Find-AzureRmResource -ResourceNameContains $ServicePlanName -ResourceGroupNameContains $ResourceGroupName -ResourceType "Microsoft.Web/ServerFarms"
    if(!$resource)
    {
        # Specify the Tier type that you would like
        $servicePlan = New-AzureRmAppServicePlan -ResourceGroupName $ResourceGroupName -Name $ServicePlanName -Location $WebAppLocation -Tier Standard -NumberofWorkers 1 -WorkerSize "Small"
    }
}

答案 1 :(得分:1)

问题是您在默认的免费应用服务计划(即服务器场或虚拟主机计划 - 它们都是一样的东西)中创建了您的站点,称为“Default1”。然而,您缩放到标准尺寸的应用服务计划是另一个名为“devHostingPlanWestEU10”的服务计划。

要在预先存在的应用服务计划中创建网站,请使用以下命令:

(为了便于阅读,分为多行)

New-AzureResource 
  -Name <YourSiteName>
  -Location "West US" 
  -ResourceGroupName  <YourResourceGroupName> 
  -ApiVersion 2014-11-01 
  -ResourceType "Microsoft.Web/sites" 
  -PropertyObject @{ "serverFarm" = "<Your service plan name>" } 

答案 2 :(得分:0)

使用New Azure Resource Manager Cmdlet。您可以创建新的应用服务计划并将其传递给New-AzureRmWebApp Cmdlet。

New-AzureRmAppServicePlan -Name StdPlan -Location&lt;“Location”&gt; -ResourceGroupName&lt;“ResourceGroupName”&gt; - Tier Standard

New-AzureRmWebApp -ResourceGroupName&lt;“ResourceGroupName”&gt; -Name&lt;“WebAppname”&gt; -Location&lt;“Location”&gt; -AppServicePlan StdPlan

参考文章:https://docs.microsoft.com/en-us/azure/app-service-web/app-service-web-app-azure-resource-manager-powershell