powershell - 通过 Powershell 创建具有标准定价层的 Azure 网站

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

我正在尝试修复我的连续部署方案,为此,Azure 网站必须存在并且能够在暂存和生产之间切换。我们希望该网站能够在标准定价层运行。

我现在的脚本创建了一个新的资源组、托管计划,在创建这些之后,创建了网站本身。我面临的问题是网站始终处于免费模式。我应该能够使用 Set-AzureResource cmdlet 来解决此问题,但是这个 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,所以我会遇到同样的问题。此示例的另一个问题是您无法控制创建网站的资源组和托管计划,这会在搜索网站时造成一些麻烦。

最佳答案

这在新的 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"
    }
}

关于powershell - 通过 Powershell 创建具有标准定价层的 Azure 网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30373171/

相关文章:

azure - 无法在管道中使用 AZ CLI 下载 blob - SAS 值已从 SAS token 中删除

azure - 许多 Azure 应用服务的部署方法

arrays - PowerShell 中的字符串比较似乎不起作用

windows - 相当于 BASH(等) 'type' 命令的 PowerShell?

powershell - 如何使用 Azure Blob 存储中的现有 VHD 创建新 VM?

azure - 通过 JDBC 访问 Azure HDInsights 中的 Spark

asp.net - ILoggingBuilder 没有参数为 0 的 AddEventSourceLogger。如何初始化此日志记录

c# - 将凭据从 Web 应用程序传递到 Web API。 Azure AD 身份验证

powershell - Windows7 中的 Add-PsSnapin WebAdministration

powershell - PowerShell中数字后缀的完整列表是什么?