<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>web.config &#8211; Tjitjing Blog</title>
	<atom:link href="http://localhost/index.php/tag/web-config/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.tjitjing.com/</link>
	<description>This is my blog on technology related stuff and on occation other things too...</description>
	<lastBuildDate>Sat, 27 Aug 2011 13:01:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.16</generator>
	<item>
		<title>Automate update of web.config with Microsoft Powershell</title>
		<link>https://blog.tjitjing.com/index.php/2011/08/automate-update-of-web-config-with-microsoft-powershell.html</link>
					<comments>https://blog.tjitjing.com/index.php/2011/08/automate-update-of-web-config-with-microsoft-powershell.html#comments</comments>
		
		<dc:creator><![CDATA[Max]]></dc:creator>
		<pubDate>Sat, 27 Aug 2011 13:01:53 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Windows Server 2008]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[web.config]]></category>
		<category><![CDATA[Windows tools]]></category>
		<guid isPermaLink="false">http://blog.tjitjing.com/?p=431</guid>

					<description><![CDATA[I have an ASP.NET web site project that I use for 5-10 web sites. Each has its own directory, setup in IIS and MySQL database, ie they work as independent web sites, only the code base is the same. For updates I have a setup with one Windows command file to copy out any file&#8230; <a class="more-link" href="https://blog.tjitjing.com/index.php/2011/08/automate-update-of-web-config-with-microsoft-powershell.html">Continue reading <span class="screen-reader-text">Automate update of web.config with Microsoft Powershell</span></a>]]></description>
										<content:encoded><![CDATA[<p>I have an ASP.NET web site project that I use for 5-10 web sites. Each has its own directory, setup in IIS and MySQL database, ie they work as independent web sites, only the code base is the same. For updates I have a setup with one Windows command file to copy out any file changes to the web site directories and another command file that updates the database with any database structure changes. </p>
<p><strong>So far I have made any updates to the web.config files manually but today I finally figured out how to automate this too.</strong></p>
<p>The Windows command tool Powershell has been around for a couple of years but I have not used it before. It was installed with Windows Update some time ago so it should be installed on your machine. To use it simply run powershell.exe from a command prompt. To exit, close the window or type exit.<br />
Powershell can among other things run Powershell script files (file extension .ps1)</p>
<p><strong>This script will show you how to add to and edit multiple web.config files (or any xml file)</strong><br />
[code lang=&#8221;xml&#8221;]<br />
# Source: http://blog.tjitjing.com/</p>
<p># Array of files to make changes to, add as many as you like<br />
$filesarray = @(&quot;c:\temp\web.config&quot;,&quot;c:\anotherfile.xml&quot;) </p>
<p># Go thru all files<br />
foreach ($filename in $filesarray)<br />
{<br />
	# Get file + cast as xml<br />
	$xml = [[xml]](get-content $filename)</p>
<p>	# Backup file before making changes<br />
	$backup = $filename + &quot;-&quot; + (get-date).tostring(&quot;yyyy-MM-dd-hh_mm_s&quot;)<br />
	$xml.Save($backup)</p>
<p>	$root = $xml.get_DocumentElement();</p>
<p>	# Add a new node<br />
	$node = $xml.createElement(&quot;mynewnode&quot;)<br />
	$root.&quot;system.web&quot;.appendChild($node)<br />
	$subnode = $xml.createElement(&quot;add&quot;)<br />
	$node.AppendChild($subnode)<br />
	$attribute = $xml.CreateAttribute(&quot;url&quot;)<br />
	$attribute.set_Value(&quot;http://blog.tjitjing.com&quot;)/<br />
	$subnode.SetAttributeNode($attribute )</p>
<p>	# Change existing node<br />
	$root.connectionStrings.add.connectionString = &quot;My new connection string&quot;</p>
<p>	# Save<br />
	$xml.Save($filename)<br />
}<br />
[/code]</p>
<p>There are two caveats I ran in to when running my script:</p>
<p>1) You need to always give the path of the file, even if it is in your current directory. To run a file from current directory you need to wite ./<br />
[code lang=&#8221;vb&#8221;]<br />
C:\Temp&gt;powershell.exe ./test.ps1<br />
[/code]<br />
Or from within Powershell<br />
[code lang=&#8221;vb&#8221;]<br />
PS C:\Temp&gt;./test.ps1<br />
[/code]</p>
<p>2) Powershells execution policy is by default set to Restricted. To avoid getting a message similar to &#8220;File C:\temp\test.ps1 cannot be loaded because the execution of scripts is disabled on this system.&#8221;, you need to run the command <code>Set-ExecutionPolicy RemoteSigned</code> from the powershell prompt. </p>
<p>Some useful links that helped me understand this and get it working:<br />
<a href="http://technet.microsoft.com/en-us/library/ee692944.aspx" target="_blank" rel="noopener noreferrer">Powershell on Microsoft Technet</a><br />
<a href="http://get-powershell.com/post/2008/07/29/Editing-WebConfig-Files-with-PowerShell.aspx" target="_blank" rel="noopener noreferrer">Andy Schneider&#8217;s Blog about Windows PowerShell</a><br />
<a href="http://arcware.net/modifying-web-config-with-powershell/" target="_blank" rel="noopener noreferrer">Blog post by Dave Donaldson</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.tjitjing.com/index.php/2011/08/automate-update-of-web-config-with-microsoft-powershell.html/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
