带参数的 Set-WebConfigurationProperty

Set-WebConfigurationProperty with parameters

如果我运行下面的脚本;在 MySite 上创建了一个名为 RuleName 的规则,并为其设置了匹配 URL 规则:

$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 '(.*)';

如果我 运行 这个脚本将规则名称参数化或将整个 -filter 作为参数,我会收到以下错误:

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

规则是在 Add-WebConfigurationProperty 部分创建的,但之后我无法对其进行编辑。

我在参数化属性中是否遗漏了一些会破坏此功能的内容?

我为任何感兴趣的人解决了这个问题。

看起来 Set-WebConfigurationPropertyAdd-WebConfigurationProperty 处理规则名称的方式不同。对于添加,名称 'RuleName' 将创建一个名为“'RuleName'”的规则,然后在编辑规则 'RuleName'(参数化)时搜索 'RuleName' 没有引用但没有找到它。我通过如下两个参数解决了这个问题:

$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 (.*); 

这按预期工作。