【问题标题】:How to update an Azure Cloud Service setting using Azure Powershell如何使用 Azure Powershell 更新 Azure 云服务设置
【发布时间】:2014-11-23 10:30:01
【问题描述】:

是否可以使用 Azure Powershell 更新 Azure 云服务中的设置值?

【问题讨论】:

    标签: azure azure-web-roles


    【解决方案1】:

    到目前为止,还没有办法只更新一个设置(服务管理 API 不允许这样做 - 它只接受整个服务配置)。因此,为了更新单个设置,您必须更新整个配置。你可以用 PowerShell 做到这一点:

    # Add the Azure Account first - this will create a login promppt
    Add-AzureAccount 
    # when you have more then one subscription - you have explicitly select the one 
    # which holds your cloud service you want to update
    Select-AzureSubscription "<Subscription name with spaces goes here>"
    # then Update the configuration for the cloud service
    Set-AzureDeployment -Config -ServiceName "<cloud_service_name_goes_here>" `
                        -Configuration "D:/tmp/cloud/ServiceConfiguration.Cloud.cscfg" `
                        -Slot "Production"
    

    对于 `-Configuration' 参数,我提供了我想与我的云服务一起使用的新配置文件的完整本地路径。

    这是经过验证且有效的解决方案。

    【讨论】:

      【解决方案2】:

      正如 asaykov 所说,您无法使用 Powershell 更新单个云配置值。

      但是您可以读取所有设置,更新您想要更改的设置,将其保存到临时文件,然后再次设置所有设置,如下所示:

      UpdateCloudConfig.ps1:

      param
      (
          [string] $cloudService,
          [string] $publishSettings,
          [string] $subscription,
          [string] $role,
          [string] $setting,
          [string] $value
      )
      
      # param checking code removed for brevity
      
      Import-AzurePublishSettingsFile $publishSettings -ErrorAction Stop | Out-Null
      
      function SaveNewSettingInXmlFile($cloudService, [xml]$configuration, $setting, [string]$value)
      {
          # get the <Role name="Customer.Api"> or <Role name="Customer.NewsletterSubscription.Api"> or <Role name="Identity.Web"> element
          $roleElement = $configuration.ServiceConfiguration.Role | ? { $_.name -eq $role }
      
          if (-not($roleElement))
          {
              Throw "Could not find role $role in existing cloud config"
          }
      
          # get the existing AzureServiceBusQueueConfig.ConnectionString element
          $settingElement = $roleElement.ConfigurationSettings.Setting | ? { $_.name -eq $setting }
      
          if (-not($settingElement))
          {
              Throw "Could not find existing element in cloud config with name $setting"
          }
      
          if ($settingElement.value -eq $value)
          {
              Write-Host "No change detected, so will not update cloud config"
              return $null
          }
      
          # update the value 
          $settingElement.value = $value
      
          # write configuration out to a file
          $filename = $cloudService + ".cscfg"
          $configuration.Save("$pwd\$filename")
      
          return $filename
      }
      
      Write-Host "Updating setting for $cloudService" -ForegroundColor Green
      
      Select-AzureSubscription -SubscriptionName $subscription -ErrorAction Stop  
      
      # get the current settings from Azure
      $deployment = Get-AzureDeployment $cloudService -ErrorAction Stop
      
      # save settings with new value to a .cscfg file
      $filename = SaveNewSettingInXmlFile $cloudService $deployment.Configuration $setting $value
      
      if (-not($filename)) # there was no change to the cloud config so we can exit nicely
      {
          return
      }
      
      # change the settings in Azure
      Set-AzureDeployment -Config -ServiceName $cloudService -Configuration "$pwd\$filename" -Slot Production
      
      # clean up - delete .cscfg file
      Remove-Item ("$pwd\$filename")
      
      Write-Host "done"
      

      【讨论】:

        猜你喜欢
        • 2023-03-22
        • 2011-01-28
        • 1970-01-01
        • 1970-01-01
        • 2016-06-12
        • 1970-01-01
        • 1970-01-01
        • 2019-09-14
        • 1970-01-01
        相关资源
        最近更新 更多