使用Powershell设置配置值

时间:2018-10-04 15:29:36

标签: azure powershell

我有以下代码:

#Set the .config settings
$appSettings = (Get-AzureWebsite -Name $webAppName -Slot $SlotName).AppSettings
$conStrings = (Get-AzureWebsite -Name $webAppName -Slot $SlotName).ConnectionStrings    

$xmlDocument = New-Object XML
$path = (Get-ChildItem -Path ${location} -Filter "*.exe.config" -Recurse).Fullname
$xmlDocument.Load($path)

foreach($setting in $XmlDocument.configuration.appSettings.ChildNodes)
{
  $setting.Value = $appSettings[$setting.Key].Value
}

$xmlDocument.Save($path)

运行此命令时,出现以下错误(上下文是八达通)

Invoke-Expression : Cannot set "value" because only strings can be used as values to set XmlNode properties.

我似乎无法弄清楚,因为我期望Value是一个字符串。我试图将xml中的值设置为与Azure仪表板应用程序设置(和配置字符串)中的值相同,但是我无法使其正常工作。

如何正确设置每个设置?

1 个答案:

答案 0 :(得分:0)

这最终对我有用:

#Set the .config settings
$appSettings = (Get-AzureWebsite -Name $webAppName -Slot $SlotName).AppSettings
$conStrings = (Get-AzureWebsite -Name $webAppName -Slot $SlotName).ConnectionStrings

#Load xml config file
$xmlDocument = New-Object XML
$path = (Get-ChildItem -Path ${location} -Filter "*.exe.config" -Recurse).Fullname
$xmlDocument.Load($path)

#Update settings from appSettings
foreach($setting in $appSettings.GetEnumerator())
{

    $targetNode = $XmlDocument.SelectSingleNode("//configuration/appSettings/add[@key='$($setting.Name)']")
    if($targetNode -ne $null)
    {
        Write-Host "Target: $($targetNode.Key) being set to $($setting.Value)"
        $targetNode.Value = $setting.Value
    }   
    else
    {
        Write-Host "Target Node was null"
        Write-Host "Setting was Key: $($setting.Name) Value: $($setting.Value)"
    }
}

#Update connection string section
foreach($setting in $conStrings.GetEnumerator())
{
    $targetNode = $XmlDocument.SelectSingleNode("//configuration/connectionStrings/add[@name='$($setting.Name)']")
    if($targetNode -ne $null)
    {
        Write-Host "Target: $($targetNode.Name) being set to $($setting.connectionString)"
        $targetNode.connectionString = $($setting.connectionString)
    }   
    else
    {
        Write-Host "Target Node was null"
        Write-Host "Setting was Key: $($setting.Name) Value: $($setting.connectionString)"
    }
}

$xmlDocument.Save($path)

我对Powershell还是很陌生,在执行此操作的过程中一定犯了所有错误,但现在已完成,我学到了一些有用的知识。 \ 0 /