You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用IISAdministration PowerShell模块添加请求动词?

背景

我正在为IIS 10编写符合CIS基准的审计与加固脚本。我了解可通过旧版WebAdministration模块或appcmd可执行文件完成此操作,但我尽可能尝试使用IISAdministration模块实现。

问题

我正尝试应用更改以满足CIS基准“4.6 确保禁用HTTP Trace方法”的要求。我能够获取动词配置,但目前无法向requestFiltering添加动词。

我可以通过以下方式获取动词:

$requestConfigSection = Get-IISConfigSection -SectionPath 'system.webServer/security/requestFiltering'
$verbsElements = Get-IISConfigElement -ConfigElement $requestConfigSection -ChildElementName 'verbs'

如果trace已存在,我可以这样获取它:

$traceVerb = Get-IISConfigAttributeValue -ConfigElement $verbsElements -AttributeName 'trace'

并这样设置:

Set-IISConfigAttributeValue -ConfigElement $verbsElements -AttributeName 'trace' -AttributeValue $false

如何添加不存在的动词?

我正在寻找对应以下WebAdministration模块命令的IISAdministration模块实现:

Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.webServer/security/requestFiltering/verbs" -name "." -value @{verb='TRACE';allowed='False'}
后续尝试

我已尝试按照评论中的.NET方法操作,得到了相同的结果:

(Get-iisserverManager).GetApplicationHostConfiguration().GetSection("system.webServer/security/requestFiltering").GetChildElement('verbs')

使用GetCollection()确实能得到结果,但不符合预期:

$requestSection = (Get-iisserverManager).GetApplicationHostConfiguration().GetSection("system.webServer/security/requestFiltering")
$requestSection.GetCollection('verbs')   #返回空ConfigurationElement结果
$requestSection.GetChildElement('verbs')   #返回如下结果

Attributes      : {allowUnlisted, applyToWebDAV}
ChildElements   : {}
ElementTagName  : verbs
IsLocallyStored : True
Methods         :
RawAttributes   : {[allowUnlisted, True], [applyToWebDAV, True]}
Schema          : Microsoft.Web.Administration.ConfigurationElementSchema

对于偏好完整PowerShell结果的用户,我使用以下命令也得到了相同的结果:

$verbsCollection = Get-IISConfigCollection -ConfigElement $requestConfigSection -CollectionName 'verbs'
$verbsCollection.GetType()  #确认是ConfigurationElementCollection类型的空集合

显然它找到了一个集合(无报错),但集合为空。将其存储到变量后,可以看到它的类型为[ConfigurationElementCollection]。由于集合为空,Get-Member无法使用,但展开$verbsCollection.PSObject.methods可以看到存在Add()方法。

疑问

为什么GetChildElement能返回结果,但GetCollection却为空?我推测这是继承导致的,但我已经从最高层级获取配置了。我或许能自行解决,但希望得到这个问题的明确答案。

内容的提问来源于stack exchange,提问作者lumberajackshaw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 06:33:11