我目前有以下代码,但显然我对使用集合并不熟练。因此,我似乎无法弄清楚如何在循环的每一轮中从规则属性中获取每个名称值。
dim c,ExistingRules
set objFWP = createobject("HNetCfg.FwPolicy2")
set colRules = objFWP.rules
for c = 1 to colRules.count - 1
ExistingRules = ExistingRules & colRules(c).name
next
代替colRules(c).name
,我尝试过这样的事情:
colRules.item(c).name
colRules.name(c)
t = colRules.name / t(c)
...等
尽管研究如何在网络上迭代一个集合而不是...每个人都在网上,并了解更多关于如何使用集合,我相信这里的社区可以帮助我找到解决方案/来源以了解更多信息快。
答案 0 :(得分:3)
Rules
对象是COM接口INetFwRules
的一个实例。事实证明,无法通过索引从此集合中检索项目。 Item
方法的定义
表明它通过名称检索规则。因此,在这种特殊情况下,您必须使用For Each ... In
。
Dim rule
For Each rule In colRules
WScript.Echo "Rule: " & rule.Name
Next
如果您想提前中断循环然后重新枚举,则需要重新分配规则集合:
Dim rule
Dim colRules
Set colRules = objFWP.Rules
WScript.Echo "Loop first time..."
For Each rule In colRules
WScript.Echo "Rule: " & rule.Name
Exit For
Next
' Reassign colRules
Set colRules = objFWP.Rules
WScript.Echo "Loop again..."
For Each rule In colRules
WScript.Echo "Rule: " & rule.Name
Exit For
Next
答案 1 :(得分:0)
set objFWP = createobject("HNetCfg.FwPolicy2")
set colRules = objFWP.rules
' because index in VBScript is zero based
for c = 0 to colRules.count - 1
'assuming name is one of the property for items in the collection
ExistingRules = ExistingRules & colRules.ItemIndex(c).name
next