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

Jan*_*n_V 6 powershell azure azure-web-sites azure-app-service-plans

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

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

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

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

#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
Run Code Online (Sandbox Code Playgroud)

我读过网站应该继承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
                    }
Run Code Online (Sandbox Code Playgroud)

这看起来对我很好.创建网站后,将打印以下属性:

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
                    }
Run Code Online (Sandbox Code Playgroud)

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

因此我可能需要更新资源,但这会抛出上面提到的错误.

我也尝试使用New-AzureWebsite,使用特洛伊亨特的博文作为一个例子.但是,这个帖子也依赖于使用Set-AzureResource,所以我会陷入同样的​​问题.此示例的另一个问题是您无法控制创建网站的资源组和托管计划,这会在搜索网站时造成一些麻烦.

小智 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"
    }
}
Run Code Online (Sandbox Code Playgroud)

接下来,使用应用服务计划作为参数创建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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

$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"
    }
}
Run Code Online (Sandbox Code Playgroud)