Adamon
Adamon

Reputation: 534

Set-WebConfigurationProperty with parameters

If I run the below script; a rule is created on MySite called RuleName and the match URL rule is set for it:

$PSPath = 'IIS:\Sites\MySite'

Add-WebConfigurationProperty -PSPath $PSPath  -filter "system.webServer/rewrite/rules" -name '.' -value @{name='RuleName'; patterSyntax='Regular Expressions'; stopProcessing='True'}; 
Set-WebConfigurationProperty -PSPath $PSPath -filter "system.webServer/rewrite/rules/rule[@name='RuleName']/match" -name 'url' -value '(.*)';

If i were to run this script with the rule name parameterised OR the whole -filter as a parameter I get the following error:

WARNING: Target configuration object 'system.webServer/rewrite/rules/rule[@name='RuleName']/match is not found at path 'MACHINE/WEBROOT/APPHOST/MySite'.

The rule is created in the Add-WebConfigurationProperty section but I can't edit it after.

Is there something I'm missing in the properties about parameterisation that would break this?

Upvotes: 1

Views: 7017

Answers (1)

Adamon
Adamon

Reputation: 534

I solved this for anyone interested.

It looks like Set-WebConfigurationProperty and Add-WebConfigurationProperty handle the rule name differently. For Adding, the name 'RuleName' will create a rule called "'RuleName'" and then when it comes to editing the rule 'RuleName' (parameterised), was searching for 'RuleName' with no quotes and not finding it. I solved this by having 2 parameters as follows:

$RuleName1 = 'RuleName'
$RuleName2 = "'RuleName'"

$PSPath = 'IIS:\Sites\' + $SitePath

Add-WebConfigurationProperty -PSPath $PSPath  -filter "system.webServer/rewrite/rules" -name '.' -value @{name=$RuleName1; patterSyntax='Regular Expressions'; stopProcessing='True'}; 
Set-WebConfigurationProperty -PSPath $PSPath -filter "system.webServer/rewrite/rules/rule[@name=$RuleName2]/match" -name 'url' -value (.*); 

This works as expected.

Upvotes: 5

Related Questions