powershell
]
editing XML config files with powershell
A long, long time ago in a server room far, far away I needed to automate deployment of web services in IIS server and parametrize the web services via script.
This lead me to creation of Edit-Config.ps1 which I published also in Technet Gallery (which was retired 12/2020).
Recently, I needed to use that script again, so I decided to publish it in PowerShell gallery. In this post, I will provide:
- Steps to install the module/script.
- Common scenarios where the script can be useful.
- Notes on changes made since the original version prepared in 2015.
BEWARE Please be cautious when using this script and thoroughly test it before implementation.
Nevertheless, I hope it proves helpful to someone .
Set-Config module installation
Simply:
Install-Module Set-Config
Usage
To edit an XML config file, follow these steps:
- Backup the Original File: Before making any changes, create a backup of the original XML config file to ensure you can revert back if needed.
- Determine the XPath: Identify the XPath expression that targets the specific attribute, value, or element text you want to modify within the XML file.
-
Run Set-Config: Utilize the
Set-Config
(formerlyEdit-Config
) script to programmatically update the XML config file based on the specified XPath and desired changes. - Verify the Contents: After running the script, verify the updated contents of the XML file. You can use tools like WinMerge or VSCode.
For XPath determination, consider using XML-specific tools available for text editors like Visual Studio Code with XML Tools plugin, or Notepad++ with XML Tools.
Determining XPath is 99% of the magic you need to know. Here are some resources for a better start.
It also covers 50% of your problems with editing the XML config. The remaining 50% is that you did not backup the original XML config. So don’t forget that .
Set-Config
cmd-let requires these parameters:
ParameterName | Description |
---|---|
ConfigFileName |
This is the path to the Xml config you want to edit (and you have backed up for sure in the first place). |
XPath |
XPath determining the node you want to edit. |
When you know the file and the XML path you also want to usually set something .
For this purpose, you can use one or more following parameter(s):
ParameterName | Description |
---|---|
Attribute |
Name of attribute you want to set in an element/node. |
Value |
Value of attribute you want to set. |
InnerText |
Inner text to set in an element/node. |
To preview changes, you can run the Set-Config
with -WhatIf
parameter.
Enough, let’s see some examples!
Example 1: Setting a certificate thumbprint in Web.config
One of common scenarios is modifying web service certificate thumbprint
parameter in the Web.config
configuration file. This is typically found in
serviceCertificate
element in attribute findValue
.
To preview the changes first, run following PowerShell command.
Set-Config -WhatIf -ConfigFileName web.config -XPath "/configuration/system.serviceModel/behaviors/serviceBehaviors/behavior/serviceCredentials/serviceCertificate" -Attribute "findValue" -Value "E483FA9FFA42F000A366773DD124CE532C31BC68"
The cmdlet will attempt to replace the value in the configuration. In the screenshot, you can see a warning indicating that a missing element in the web.config file is causing this issue.
You can safely ignore the warning if you want to create a new XML element in the config file.
To actually make a change run the same code but without WhatIf
directive.
Set-Config -ConfigFileName web.config -XPath "/configuration/system.serviceModel/behaviors/serviceBehaviors/behavior/serviceCredentials/serviceCertificate" -Attribute "findValue" -Value "E483FA9FFA42F000A366773DD124CE532C31BC68"
The cmdlet can be a little more chatty. To obtain additional information, use the -Verbose
parameter.
Example 2: Updating / setting metadata URL in application configuration section
In some cases, you may encounter identical elements that differ only in the attribute values section. This scenario is typical for key-value application configuration sections.
Consider following application configuration section:
<appSettings>
<add key="LogFile" value="C:\TEMP\application.log" />
<add key="EventLogLevel" value="3" />
<add key="FileLogLevel" value="3" />
</appSettings>
To change the FileLogLevel
you can use this XPath:
/configuration/appSettings/add[@key="FileLogLevel"]
The entire cmdlet would than look like this:
Set-Config -ConfigFileName web.config -XPath '/configuration/appSettings/add[@key="FileLogLevel"]' -Attribute "value" -Value "4"
Example 3: Silent usage
When you want to modify the config file silently, you need to specify -Confirm:$false
parameter. E.g.
Set-Config -Confirm:$false -ConfigFileName web.config -XPath '/configuration/appSettings/add[@key="FileLogLevel"]' -Attribute "value" -Value "4"
Few notes on changes
Set-Config
differs from Edit-Config
only in name.
I’ve changed the name to better reflect what the cmd-let does and decided to use one of the approved verbs for PowerShell commands.
I’ve included alias Edit-Config
for backwards compatibility.
Note to self: naming things is hard.