Modify IIS web.config using PowerShell

I'm trying to modify the web.config using PowerShell.

So far, I'm able to replace the Connection String using:

Set-WebConfigurationProperty -pspath "IIS:\Sites\$Name\WS" -filter "connectionStrings/add[@name='Product.Core.WebServices.Properties.Settings.DirectoryConnectionString']" -name "connectionString" -value "Server=SERVERNAME\INSTANCE1;Database=ProductDirectory.$Name;Trusted_Connection=True;"

this works OK.

Now, I want to change something in the applicationSettings part of the web.config. The Part looks like:

<applicationSettings>
    <Product.Core.WebServices.Properties.Settings>
      <setting name="LogLevel" serializeAs="String">
        <value>Info</value>
      </setting>
      <setting name="LogDirectory" serializeAs="String">
        <value>D:\temp</value>
      </setting>
      <setting name="LogSpecialTroubleshoot" serializeAs="String">
        <value>False</value>
      </setting>
      <setting name="LogSqlQueries" serializeAs="String">
        <value>False</value>
      </setting>
      <setting name="LogPerformance" serializeAs="String">
        <value>False</value>
      </setting>
      <setting name="LogXmlInput" serializeAs="String">
        <value>False</value>
      </setting>
      <setting name="LogXmlInputSeperate" serializeAs="String">
        <value>False</value>
      </setting>
      <setting name="EnableCatchAllLogging" serializeAs="String">
        <value>True</value>
      </setting>
      <setting name="DirectoryDatabaseName" serializeAs="String">
        <value>ProductDirectory</value>
      </setting>
    </Product.Core.WebServices.Properties.Settings>   
</applicationSettings>

I would like to change the "DirectoryDatabaseName" node.

Using:

Set-WebConfigurationProperty -pspath "IIS:\Sites\$Name\WS" -filter "applicationSettings" -name "test" -value "ProductDirectory.$Name"

I can change the "root" node (applicationSettings) and add an attribute with name "test" to it. However, if I'm trying to go "deeper" into the structure, I get the following Error:

WARNING: Target configuration object 'applicationSettings/Product.Core.WebServices.Properties.Settings/setting[@name='DirectoryDatabaseName'] is not found a
t path 'MACHINE/WEBROOT/APPHOST/TestM/WS'.

I'm using the following PS for that:

Set-WebConfigurationProperty -pspath "IIS:\Sites\$Name\WS" -filter "applicationSettings/Product.Core.WebServices.Properties.Settings/setting[@name='DirectoryDatabaseName']" -name "test" -value "ProductDirectory.$Name"

Not even this works (same error):

Set-WebConfigurationProperty -pspath "IIS:\Sites\$Name\WS" -filter "applicationSettings/Product.Core.WebServices.Properties.Settings" -name "test" -value "ProductDirectory.$Name"

So somehow it stops working "one level below" applicationSettings

Also, how would the syntax be to change the text between <value> and </value>?


Solution 1:

The following worked for me when editing a web.config file in a virtual directory of the default website and trying to view and change the appSettings section:

#List all appSettings
Get-WebConfigurationProperty -pspath "iis:\Sites\Default Web Site\VirtualDir" -filter "/appSettings/add" -name *

#Update specific appSetting
Set-WebConfigurationProperty -pspath "iis:\Sites\Default Web Site\VirtualDir" -filter "/appSettings/add[@key='SomeKey']" -name value -Value "SomeValue"

This assumes a standard web.config appSettings section like so:

<configuration>
    ...
    <appSettings>
        ...
        <add key="TestKey" value="TestValue"></add>
        ...
    </appSettings>
    ...
</configuration>

In your case, the value is stored as a text node instead of an attribute, so the full xpath would be /applicationSettings/Product.Core.WebServices.Properties.Settings/setting[@name='DirectoryDatabaseName']/value/text() however I cannot find any example of web.config files with values stored as text nodes. The examples for the IIS cmdlets also don't show any references to text().

You may have to set the filter to retrieve the settings element, and then set it to a value element with the correct text. Again the examples don't show how to do this, but they do show how to use a hashtable to set multiple values, so you may be able to find a way to make it work.