How to update an Azure Cloud Service setting using Azure Powershell

不问归期 提交于 2019-12-04 04:13:56

问题


Is it possible to update the value of a setting in an Azure Cloud Service with Azure Powershell?


回答1:


So far there is no way to update just a single setting (the Service Management API does not allow it - it only accepts the whole service configuration). So, in order to update a single setting, you will have to update the entire configuration. And you can do this with 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"

For the the `-Configuration' parameter I have provided full local path to the new config file I want to use with my cloud service.

This is verified and working solution.




回答2:


As astaykov says, you can't update a single cloud config value using Powershell.

But you can read all of the settings, update the one you wish to change, save it to a temp file, and then set all the settings again, like so:

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"


来源:https://stackoverflow.com/questions/26092979/how-to-update-an-azure-cloud-service-setting-using-azure-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!