用于更新 web.config 文件中 system.web 部分下的会话存储部分的 Powershell 脚本

Powershell script to update session store section under system.web section in a web.config file

...
<system.web>
    ...other nodes..

    <sessionState timeout="10" mode="Custom" customProvider="PROVIDER_NAME">
      <providers>
        <add name="PROVIDER_NAME" type="PROVIDER_TYPE" throwOnError="true" retryTimeoutInMilliseconds="5000" databaseId="0" applicationName="AppNAME" connectionString="CONNECTION_STRING" />
      </providers>
    </sessionState>

    ..other nodes...
  </system.web>

我在 web.config 文件中有上述条目。我喜欢使用 powershell 脚本更新上述节点中的连接字符串。更改应该只影响连接字符串,而不影响任何其他 nodes/attributes。我的应用程序托管在 Azure 中。我知道连接到 azure 应用程序并更新应用程序设置,如下所示。

$app = Set-AzureRMWebApp -Name $name -ResourceGroupName $group **-AppSettings $mysettingsCollection** -- this works fine

但我不确定是否要更新 xpath 中的特定节点。如果可能,请提供一些示例脚本。类似的东西 ->

$app = Set-AzureRMWebApp -Name $name -ResourceGroupName $group -**SYSTEM.WEB/SESSIONSATE VALUES**

为了在维护所有其他连接字符串的同时更新单个 Azure Web Apps 连接字符串,您需要使用 Set-AzureRmWebApp cmdlet 在保存调用中传递所有连接字符串的值。为此,您必须首先遍历现有连接字符串的整个列表并将它们添加到哈希集合,然后再在哈希集合上添加或设置特定的连接字符串值。

请确保在更新 Azure Web 应用程序上的一个或多个连接字符串时删除 none 个现有连接字符串:

# Load Existing Web App settings
$webApp = Get-AzureRmWebAppSlot -ResourceGroupName "MyResourceGroup" -Name "MyWebApp" -Slot production

# Get reference to the existing Connection Strings
$existingConnectionStrings = $webApp.SiteConfig.ConnectionStrings

# Create Hash variable for Connection Strings
$hash = @{}

# Copy over all Existing Connection Strings to the Hash
ForEach($connString in $existingConnectionString) {
$hash[$connString.Name] = @{ Type = $connString.Type.ToString(); Value = $connString.ConnectionString }
}

# Add or Update a desired Connection String within the Hash collection
$hash["AppConnString"] = @{ Type = "SqlAzure"; Value = "conn-string-here" }

# Save Connection String to Azure Web App
Set-AzureRmWebAppSlot -ResourceGroupName "MyResourceGroup" -Name "MyWebApp" -Slot production -ConnectionStrings $hash

这是使用 PowerShell cmdlet 在 Azure Web 应用程序上添加或更新连接字符串时要记住的重要提示。如果您忘记了,您可能只是不小心删除了应用程序的所有其他连接字符串,因为现有的连接字符串多于您添加或更新的连接字符串。不要破坏生产,请记住使用这个提示!

有关详细信息,请参阅此博客 post:Easily Manage Azure Web App Connection Strings using PowerShell

希望对您有所帮助。

Powershell 不会让你这样做。正确完成这项工作的方法是通过 XDT 转换。 You can read more about using XDT transforms here

需要了解的是,对于应用服务应用程序,有一个配置对象是 web.config 的子集。所以像应用程序设置、连接字符串和其他一些公共部分都是这个配置对象的一部分。

这样,您可以添加 "app settings",或使用您在应用配置中的内容覆盖 web.config 中的项目。

特别是会话状态,这不是我们在应用程序配置中的元素。所以这里的回退是 web.config 因此需要 XDT 转换。

在这种情况下,XDT 转换类似于:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.web>
        <sessionState timeout="10" mode="Custom" customProvider="PROVIDER_NAME">
            <providers>
                <add name="PROVIDER_NAME" type="PROVIDER_TYPE" throwOnError="true" retryTimeoutInMilliseconds="5000" databaseId="0" applicationName="AppNAME" connectionString="CONNECTION_STRING" />
          </providers>
        </sessionState>
    </system.web>
</configuration>