使用WebConfigPropertyCollection配置HSTS时遇GetAddElementSchema索引错误
WebConfigPropertyCollection 「Invalid index」错误解决
环境信息
- Windows Server 2019
- PowerShell 5.1
- WebAdministrationDsc 4.1.0
- 涉及资源:WebConfigPropertyCollection
问题描述
为配置多个HSTS属性,按要求分两次调用WebConfigPropertyCollection资源,在psd1数据文件中定义数组并循环调用,但执行时抛出错误:
Exception calling GetAddElementSchema with 1 argument(s): Invalid index.
用到的代码片段
psd1中的数组定义:
("HttpCustomHeaderSts","IIS:\Sites\Default Web Site","system.applicationHost/sites","site","hsts","enabled","true","includeSubDomains","true","Present"), ("HttpCustomHeaderSts2","IIS:\Sites\Default Web Site","system.applicationHost/sites/site","site","hsts","enabled","true","redirectHttpToHttps","true","Present"),
配置中的循环调用逻辑:
$ConfigurationData.NonNodeData.Roles.([String]($Node.Role -eq "WEB")).ConfigColls.foreach({ WebConfigPropertyCollection $_[0] { WebsitePath = $_[1] Filter = $_[2] CollectionName = $_[3] ItemName = $_[4] ItemKeyName = $_[5] ItemKeyValue = $_[6] ItemPropertyName = $_[7] ItemPropertyValue = $_[8] Ensure = $_[9] } })
解决办法
问题出在第二个数组项的Filter参数取值上:
- 错误值:
system.applicationHost/sites/site - 正确值:
system.applicationHost/sites
WebConfigPropertyCollection资源的Filter需要指向正确的IIS配置节点路径,配置站点级HSTS属性时,正确的路径就是system.applicationHost/sites,多追加/site会导致资源无法定位到正确节点,触发索引错误。
修正后的数组定义:
("HttpCustomHeaderSts","IIS:\Sites\Default Web Site","system.applicationHost/sites","site","hsts","enabled","true","includeSubDomains","true","Present"), ("HttpCustomHeaderSts2","IIS:\Sites\Default Web Site","system.applicationHost/sites","site","hsts","enabled","true","redirectHttpToHttps","true","Present"),
内容的提问来源于stack exchange,提问作者QuilleyPowers
相关产品推荐
相关产品推荐

