Thursday, July 21, 2016

Script for updating URLrewrite-rules

In these days urlrewrite might not be the first choice for using but I had a situationen where i needed to be applied.
This offered an oppertunity to create a script to automate the addition of custom rules sets for a specific file.
And I got to fiddle with xml-manipulation which is also fun.
This script reads an csv for inputaddress and ruleTemplate file
csv – addresses.csv
ruleName,target,destination
tomteland,www.tomteland.se,tomteland
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="#RuleName#_RevProxyInbound" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="#target#" />
</conditions>
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
<action type="Rewrite" url="http://mysite.company.com/#destination#/{R:1}" />
</rule>
</rules>
<outboundRules>
<rule name="#RuleName#_Links_To_Pages" preCondition="ResponseIsHtml" enabled="true">
<match filterByTags="A" pattern="(.*)/#destination#/(.*).aspx" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="http://#target#/{R:2}.aspx" />
</rule>
<rule name="#RuleName#_Response_Location_querystring" preCondition="ResponseIsHtml" enabled="true">
<match serverVariable="RESPONSE_LOCATION" pattern="http://mysite.company.com/#destination#/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{URL}" negate="true" pattern="\.axd$" />
</conditions>
<action type="Rewrite" value="{R:1}" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>

Powershellscript, this script reads web.config from designated place makes a copy to $workingDir where updates are applied to file in $workingDir. This a pretty specific script, for example outboundRules are applied before <precondition>-tagg, but haven’t really experimented what happens if the preconditions aren’t there for example. So better to use as inspiration rather than copy paste. Also rewriteRules will need to be adjusted for your particuliar scenarios. Matching for rules to change are made with replace() towards #rulename#, #destination and #target# cause this was easier for this scenario.
#created:2016-07-21
#lastModified:2016-07-21
#Desc: Adds rewrite rules for washers, only adds if rules doesn't exist.
$sourceFileRewrite = "C:\Eriks\Powershell\Prod_rewriteUrlsUpdate\From\rewrite\web.config"
$scriptPath = $(split-path -Parent $myinvocation.MyCommand.Definition)
$AddressUrlFile = "$($scriptPath)\Addresses.csv"
$workingDir = "$($scriptPath)\UpdatedFiles"
$templateRuleFile = "$($scriptPath)\rewriteTemplate.xml"
#step1 copy files
try {
$fileRewrite = "$($workingDir)\$(($sourceFileRewrite.Split('\'))[-2])\$(($sourceFileRewrite.Split('\'))[-1])"
if ((Test-Path -Pathtype container -Path $($fileRewrite.TrimEnd("web.config"))) -eq $false)
{
New-Item -ItemType directory -Force -Path $($fileRewrite.TrimEnd("web.config")) |Out-Null
}
Copy-Item -Path $sourceFileRewrite -Destination $fileRewrite
write-output "Copied to $($fileRewrite) successfully"
}
catch {}
#step2 read files
[xml]$xmlRewriteInput = (Get-Content $fileRewrite -ReadCount -1)
$AddressCollection = import-csv -Delimiter "," -Path $AddressUrlFile -Encoding UTF8
$templateRuleStatic = (Get-Content $templateRuleFile -raw)
$rewriteNodes = $xmlRewriteInput.configuration.'system.webServer'
foreach ($address in $AddressCollection ) {
write-output "Processing $($address.rulename)..."
#step3 update files
$tempTemplate = $templateRuleStatic.Replace("#RuleName#",$address.ruleName).Replace("#destination#",$address.destination).replace("#target#",$address.target)
[xml]$tempXMLTemplate = $tempTemplate
foreach ($node in $tempXMLTemplate.configuration.'system.webServer'.rewrite.rules.rule) {
#check if exists
if ($rewriteNodes.rewrite.rules.rule.name -notcontains $node.name) {
$extractedInbound = $xmlRewriteInput.ImportNode(($node),$true)
$xmlRewriteInput.configuration.'system.webserver'.rewrite.rules.AppendChild($extractedInbound) |Out-Null
}
else { write-warning "Rule $($node.name) already exists in file! Skipping..." }
}
foreach ($secondNode in $tempXMLTemplate.configuration.'system.webServer'.rewrite.outboundRules.rule) {
if ($rewritenodes.rewrite.outboundRules.rule.name -notcontains $secondNode.name) {
$extractedInbound = $xmlRewriteInput.ImportNode(($secondNode),$true)
$before = $xmlrewriteInput.configuration.'system.webserver'.rewrite.outboundrules.preconditions
$xmlRewriteInput.configuration.'system.webserver'.rewrite.outboundRules.InsertBefore($extractedInbound,$before) |out-null
}
else { write-warning "Rule $($secondNode.name) already exists in file! Skipping..." }
}
try {
$xmlRewriteInput.save($fileRewrite)
write-output "Updated file saved to $fileRewrite"
}
catch {}
}

No comments:

Powershell and Uptimerobot

Uptimerobot can be quite tedious when you need to update many monitors at once. For example say you bought the license for Uptimerobot and n...