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


<?xml version="1.0" encoding="UTF-8"?>
        <rule name="#RuleName#_RevProxyInbound" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTP_HOST}" pattern="#target#" />
                        <set name="HTTP_ACCEPT_ENCODING" value="" />
                    <action type="Rewrite" url="{R:1}" />
                <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 name="#RuleName#_Response_Location_querystring" preCondition="ResponseIsHtml" enabled="true">
                    <match serverVariable="RESPONSE_LOCATION" pattern="*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
            <add input="{URL}" negate="true" pattern="\.axd$" />
                    <action type="Rewrite" value="{R:1}" />
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.

#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#",$
    [xml]$tempXMLTemplate = $tempTemplate   
    foreach ($node in $tempXMLTemplate.configuration.'system.webServer'.rewrite.rules.rule) {
        #check if exists
        if ($ -notcontains $ {
            $extractedInbound = $xmlRewriteInput.ImportNode(($node),$true)     
            $xmlRewriteInput.configuration.'system.webserver'.rewrite.rules.AppendChild($extractedInbound)  |Out-Null
        else { write-warning "Rule $($ already exists in file! Skipping..." }
    foreach ($secondNode in $tempXMLTemplate.configuration.'system.webServer'.rewrite.outboundRules.rule) {
        if ($ -notcontains $ {
            $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 $($ already exists in file! Skipping..." }
    try {
        write-output "Updated file saved to $fileRewrite"
    catch {}

No comments: