For loop VbScript中的枚举值引发错误
我需要在一个子项的所有值的数据中找到一个For loop VbScript中的枚举值引发错误,for-loop,vbscript,foreach,registry,firewall,For Loop,Vbscript,Foreach,Registry,Firewall,我需要在一个子项的所有值的数据中找到一个部分文本(不是全文匹配),或者换句话说:我需要在注册表中找到一个防火墙异常 我的问题是,在我的脚本中,For的表示arrSubKeys对象不是集合,而是集合!(方法本身实例一个新数组,不是吗?) 因此,我无法循环这些值,也不知道为什么会出现此错误,因为我直接从Technet中获取它 我需要帮助来解决这个问题,并执行“查找数据中的部分文本”命令 更新: 我发现我的错误,我试图枚举键而不是值,那么现在如何继续匹配每个值的数据中的部分文本 objReg.Enum
部分文本(不是全文匹配),或者换句话说:我需要在注册表中找到一个防火墙异常
我的问题是,在我的脚本中,For
的表示arrSubKeys
对象不是集合,而是集合!(方法本身实例一个新数组,不是吗?)
因此,我无法循环这些值,也不知道为什么会出现此错误,因为我直接从Technet中获取它
我需要帮助来解决这个问题,并执行“查找数据中的部分文本”命令
更新:
我发现我的错误,我试图枚举键而不是值,那么现在如何继续匹配每个值的数据中的部分文本
objReg.EnumValues HKLM, Key, arrValues
For Each Value in arrValues
...
最后,我完成了它,一个通用使用脚本,用于从windows上下文菜单启动它(例如),以检查指定文件的入站或出站连接块的状态:
' By Elektro
' Determines whether a program has the Inbound or Outbound connections blocked by the Windows Firewall rules.
'
' NOTE: Possibly this Snippet will not work under WindowsXP.
' Syntax:
' -------
' ThisScript.vbs "[Existing filepath]" "[IN|OUT]"
'
' Usage example:
' --------------
' Wscript.exe ThisScript.vbs "C:\Program.exe" IN
' Wscript.exe ThisScript.vbs "C:\Program.exe" OUT
' Error codes:
'
' 1: Missing arguments or too many arguments.
' 2: File not found.
' 3: Wrong value specified for parameter '[IN|OUT]'
' 4: Specific Error.
Option Explicit
Dim objFile ' Indicates the File Object.
Dim objReg ' Indicates the Registry Object.
Dim Root ' Indicates the root of the registry key.
Dim Key ' Indicates the registry key.
Dim MatchData ' Indicates the data to match.
Dim Values ' Indicates the registry value collection.
Dim Value ' Indicates the registry value.
Dim Data ' Indicates the registry data.
Dim DataIsMatched ' Indicates whether the data is matched.
Dim ConnectionType ' Indicates if it's an Inbound or Outbound check.
' Set the 'HKEY_LOCAL_MACHINE' as Root registry key.
Root = &H80000002
' Set the Firewall rules registry location as key.
Key = "SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\FirewallRules"
' Sets the Registry object.
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
' Argument count error handling.
If Wscript.Arguments.Count < 2 Then
' Notify the error to the user.
MsgBox "Missing arguments." & _
VBNewLine & VBNewLine & _
"Syntax:" & VBNewLine & _
"Script.vbs ""[FILE]"" ""[IN|OUT]""", _
16, "Firewall rules"
' Exit with reason: 'Missing arguments' error-code.
Wscript.Quit(1)
ElseIf Wscript.Arguments.Count > 2 Then
' Notify the error to the user.
MsgBox "Too many arguments." & _
VBNewLine & VBNewLine & _
"Syntax:" & VBNewLine & _
"Script.vbs ""[FILE]"" ""[IN|OUT]""", _
16, "Firewall rules"
' Exit with reason: 'Too many arguments' error-code.
Wscript.Quit(1)
End If
On Error Resume Next
' Set the FileObject with the file passed through the first argument.
Set objFile = Createobject("Scripting.FileSystemObject").GetFile(Wscript.Arguments(0))
' File-Error handling.
If Err.Number = 53 Then
' Notify the error to the user.
MsgBox "File not found:" & _
vbnewline & _
Wscript.Arguments(0), _
16, "Firewall rules"
' Exit with reason: 'File not found' error-code.
Wscript.Quit(2)
End If
' Set the partial data to match on each value-data.
If LCase(Wscript.Arguments(1)) = LCase("IN") Then
' Set the ConnectionType to 'Inbound'
ConnectionType = "Inbound"
' Match Inbound connection rule.
MatchData = "Action=Block|Active=TRUE|Dir=In|App=" & objFile.Path
Elseif LCase(Wscript.Arguments(1)) = LCase("OUT") Then
' Set the ConnectionType to 'Outbound'
ConnectionType = "Outbound"
' Match Outbound connection rule.
MatchData = "Action=Block|Active=TRUE|Dir=Out|App=" & objFile.Path
Else ' Wrong argument.
' Notify the error to the user.
MsgBox "Wrong value specified for parameter '[IN|OUT]'", _
16, "Firewall rules"
' Exit with reason: 'Wrong value specified for parameter '[IN|OUT]'' error-code.
Wscript.Quit(3)
End If
' Get the values.
objReg.EnumValues Root, Key, Values
' Loop through the values.
For Each Value In Values
' Get the data.
objReg.GetStringValue Root, Key, Value, Data
' Match the partial data.
If Not IsNull(Data) Then
' If partial data matched in data then...
If InStr(1, Data, MatchData, 1) Then
' Set the DataMAtched flag to 'True'.
DataIsMatched = True
' ...and stop the iteration.
Exit For
End If ' // InStr()
End If ' // IsNull()
Next ' // Value
' Error handling.
If Err.Number <> 0 Then
' Notify the error to the user.
MsgBox "Error Code: " & Err.Number & vbnewline & _
"Error Source: " & Err.Source & vbnewline & _
"Description: " & Err.Description, _
16, "Firewall rules"
' Exit with reason: 'Specific error' error-code.
Wscript.Quit(4)
End If
' This (ridiculous) conversion is needed;
' because the VBS engine prints the boolean value into a MsgBox;
' according to the OS language ( Spanish: Verdadero|Falso )
If DataIsMatched = True Then
DataIsMatched = "True"
Else
DataIsMatched = "False"
End If
' Notify the information to the user.
MsgBox "File: " & """" & objFile.Name & """" & vbnewline & _
"Type: " & ConnectionType & " connection" & vbnewline & _
"Blocked: " & DataIsMatched, _
64, "Firewall rules"
' Exit successfully.
Wscript.Quit(0)
Elektro的《代码》
'确定程序的入站或出站连接是否被Windows防火墙规则阻止。
'
'注意:此代码段可能无法在WindowsXP下工作。
'语法:
' -------
“ThisScript.vbs”[Existing filepath]“[IN | OUT]”
'
'用法示例:
' --------------
中的“Wscript.exe ThisScript.vbs”C:\Program.exe
'Wscript.exe ThisScript.vbs“C:\Program.exe”输出
'错误代码:
'
'1:缺少参数或参数太多。
'2:找不到文件。
“3:为参数“[IN | OUT]”指定的值错误
'4:特定错误。
选项显式
Dim objFile'表示文件对象。
Dim objReg'表示注册表对象。
Dim Root'表示注册表项的根。
Dim Key'表示注册表项。
Dim MatchData'表示要匹配的数据。
Dim Values'表示注册表值集合。
Dim Value'表示注册表值。
Dim Data'表示注册表数据。
Dim DataIsMatched'表示数据是否匹配。
Dim ConnectionType'表示是入站检查还是出站检查。
'将'HKEY_LOCAL_MACHINE'设置为根注册表项。
根=&H8000002
'将防火墙规则注册表位置设置为密钥。
Key=“SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\FirewallRules”
'设置注册表对象。
设置objReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\\。\root\default:StdRegProv”)
'参数计数错误处理。
如果Wscript.Arguments.Count<2,则
'将错误通知给用户。
MsgBox“缺少参数”。&_
VBNewLine&VBNewLine&_
“语法:”&VBNewLine&\u
“Script.vbs”“[FILE]”“[IN | OUT]”_
16,“防火墙规则”
'带原因退出:'缺少参数'错误代码。
Wscript.Quit(1)
ElseIf Wscript.Arguments.Count>2然后
'将错误通知给用户。
MsgBox“参数太多。”&_
VBNewLine&VBNewLine&_
“语法:”&VBNewLine&\u
“Script.vbs”“[FILE]”“[IN | OUT]”_
16,“防火墙规则”
'带原因退出:'参数太多'错误代码。
Wscript.Quit(1)
如果结束
出错时继续下一步
'使用通过第一个参数传递的文件设置FileObject。
设置objFile=Createobject(“Scripting.FileSystemObject”).GetFile(Wscript.Arguments(0))
'文件错误处理。
如果错误编号=53,则
'将错误通知给用户。
MsgBox“未找到文件:&”_
vbnewline&_
Wscript.Arguments(0)_
16,“防火墙规则”
'退出原因:'未找到文件'错误代码。
Wscript.Quit(2)
如果结束
'将部分数据设置为与每个值数据匹配。
如果LCase(Wscript.Arguments(1))=LCase(“IN”),那么
'将ConnectionType设置为'Inbound'
ConnectionType=“入站”
'匹配入站连接规则。
MatchData=“Action=Block | Active=TRUE | Dir=In | App=“&objFile.Path
Elseif LCase(Wscript.Arguments(1))=LCase(“OUT”),然后
'将ConnectionType设置为'Outbound'
ConnectionType=“出站”
'匹配出站连接规则。
MatchData=“Action=Block | Active=TRUE | Dir=Out | App=“&objFile.Path
其他人的错误论点。
'将错误通知给用户。
MsgBox“为参数“[IN | OUT]”指定的值错误”_
16,“防火墙规则”
'带原因退出:'为参数'[IN | OUT]'错误代码指定了错误值。
Wscript.Quit(3)
如果结束
'获取值。
objReg.EnumValues根、键、值
'循环遍历这些值。
对于值中的每个值
“获取数据。
objReg.GetStringValue根、键、值、数据
'匹配部分数据。
如果不为空(数据),则
'如果部分数据在数据中匹配,则。。。
如果仪表(1,数据,匹配数据,1),则
'将DataMAtched标志设置为'True'。
DataIsMatched=True
“…并停止迭代。
退出
如果“//InStr()结束,则结束
如果'//IsNull(),则结束
下一个'//值
'错误处理。
如果错误号为0,则
'将错误通知给用户。
MsgBox“错误代码:”&错误编号&vbnewline&_
“错误源:”&Err.Source&vbnewline&_
“说明:”&错误说明_
16,“防火墙规则”
'带原因退出:'特定错误'错误代码。
Wscript.Quit(4)
如果结束
"这种(荒谬的)转换是必要的,;
'因为VBS引擎将布尔值打印到MsgBox中;
“根据OS语言(西班牙语:Verdadero | Falso)
如果DataIsMatched=True,则
DataIsMatched=“True”
其他的
DataIsMatched=“False”
如果结束
'将信息通知给用户。
MsgBox“文件:&”“”“&objFile.Name&”“&vbnewline&_
“类型:”&ConnectionType&“连接”&vbnewline&_
“已阻止:”&数据已匹配_
64,“防火墙规则”
'成功退出。
Wscript.Quit(0)
mayhelp@Pankaj谢谢,但是网址对我帮助不大。
' By Elektro
' Determines whether a program has the Inbound or Outbound connections blocked by the Windows Firewall rules.
'
' NOTE: Possibly this Snippet will not work under WindowsXP.
' Syntax:
' -------
' ThisScript.vbs "[Existing filepath]" "[IN|OUT]"
'
' Usage example:
' --------------
' Wscript.exe ThisScript.vbs "C:\Program.exe" IN
' Wscript.exe ThisScript.vbs "C:\Program.exe" OUT
' Error codes:
'
' 1: Missing arguments or too many arguments.
' 2: File not found.
' 3: Wrong value specified for parameter '[IN|OUT]'
' 4: Specific Error.
Option Explicit
Dim objFile ' Indicates the File Object.
Dim objReg ' Indicates the Registry Object.
Dim Root ' Indicates the root of the registry key.
Dim Key ' Indicates the registry key.
Dim MatchData ' Indicates the data to match.
Dim Values ' Indicates the registry value collection.
Dim Value ' Indicates the registry value.
Dim Data ' Indicates the registry data.
Dim DataIsMatched ' Indicates whether the data is matched.
Dim ConnectionType ' Indicates if it's an Inbound or Outbound check.
' Set the 'HKEY_LOCAL_MACHINE' as Root registry key.
Root = &H80000002
' Set the Firewall rules registry location as key.
Key = "SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\FirewallRules"
' Sets the Registry object.
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
' Argument count error handling.
If Wscript.Arguments.Count < 2 Then
' Notify the error to the user.
MsgBox "Missing arguments." & _
VBNewLine & VBNewLine & _
"Syntax:" & VBNewLine & _
"Script.vbs ""[FILE]"" ""[IN|OUT]""", _
16, "Firewall rules"
' Exit with reason: 'Missing arguments' error-code.
Wscript.Quit(1)
ElseIf Wscript.Arguments.Count > 2 Then
' Notify the error to the user.
MsgBox "Too many arguments." & _
VBNewLine & VBNewLine & _
"Syntax:" & VBNewLine & _
"Script.vbs ""[FILE]"" ""[IN|OUT]""", _
16, "Firewall rules"
' Exit with reason: 'Too many arguments' error-code.
Wscript.Quit(1)
End If
On Error Resume Next
' Set the FileObject with the file passed through the first argument.
Set objFile = Createobject("Scripting.FileSystemObject").GetFile(Wscript.Arguments(0))
' File-Error handling.
If Err.Number = 53 Then
' Notify the error to the user.
MsgBox "File not found:" & _
vbnewline & _
Wscript.Arguments(0), _
16, "Firewall rules"
' Exit with reason: 'File not found' error-code.
Wscript.Quit(2)
End If
' Set the partial data to match on each value-data.
If LCase(Wscript.Arguments(1)) = LCase("IN") Then
' Set the ConnectionType to 'Inbound'
ConnectionType = "Inbound"
' Match Inbound connection rule.
MatchData = "Action=Block|Active=TRUE|Dir=In|App=" & objFile.Path
Elseif LCase(Wscript.Arguments(1)) = LCase("OUT") Then
' Set the ConnectionType to 'Outbound'
ConnectionType = "Outbound"
' Match Outbound connection rule.
MatchData = "Action=Block|Active=TRUE|Dir=Out|App=" & objFile.Path
Else ' Wrong argument.
' Notify the error to the user.
MsgBox "Wrong value specified for parameter '[IN|OUT]'", _
16, "Firewall rules"
' Exit with reason: 'Wrong value specified for parameter '[IN|OUT]'' error-code.
Wscript.Quit(3)
End If
' Get the values.
objReg.EnumValues Root, Key, Values
' Loop through the values.
For Each Value In Values
' Get the data.
objReg.GetStringValue Root, Key, Value, Data
' Match the partial data.
If Not IsNull(Data) Then
' If partial data matched in data then...
If InStr(1, Data, MatchData, 1) Then
' Set the DataMAtched flag to 'True'.
DataIsMatched = True
' ...and stop the iteration.
Exit For
End If ' // InStr()
End If ' // IsNull()
Next ' // Value
' Error handling.
If Err.Number <> 0 Then
' Notify the error to the user.
MsgBox "Error Code: " & Err.Number & vbnewline & _
"Error Source: " & Err.Source & vbnewline & _
"Description: " & Err.Description, _
16, "Firewall rules"
' Exit with reason: 'Specific error' error-code.
Wscript.Quit(4)
End If
' This (ridiculous) conversion is needed;
' because the VBS engine prints the boolean value into a MsgBox;
' according to the OS language ( Spanish: Verdadero|Falso )
If DataIsMatched = True Then
DataIsMatched = "True"
Else
DataIsMatched = "False"
End If
' Notify the information to the user.
MsgBox "File: " & """" & objFile.Name & """" & vbnewline & _
"Type: " & ConnectionType & " connection" & vbnewline & _
"Blocked: " & DataIsMatched, _
64, "Firewall rules"
' Exit successfully.
Wscript.Quit(0)