枚举VbScript中的值会引发错误

时间:2014-02-03 09:53:17

标签: for-loop vbscript foreach registry firewall

我需要在子项的所有值的数据中找到partial文本(不是全文匹配),换句话说:我需要在注册表中找到防火墙例外。

我遇到的问题是,在我的脚本中,For表示arrSubKeys对象不是集合,但它是! (该方法本身实例是一个新数组吗?)

所以我不能循环使用这些值,我不知道为什么我会收到此错误,因为我直接从 Technet 中的here获取了该错误。< / p>

我需要帮助才能解决这个问题并执行'在数据中查找部分文本'命令。

Const HKLM      = &H80000002
Const Key       = "SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\FirewallRules"
Const MatchData = "Action=Block|Active=TRUE|Dir=Out|App=C:\FTP Manager.exe"

Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

objReg.EnumKey HKLM, Key, arrSubKeys

For Each Subkey in arrSubKeys
 Wscript.Echo Subkey
Next

Wscript.Quit()
  

更新:

我发现我的错误是我试图枚举键而不是值,所以现在我如何继续匹配每个值的数据中的部分文本?:

objReg.EnumValues HKLM, Key, arrValues

For Each Value in arrValues

...

1 个答案:

答案 0 :(得分:0)

最后我做了一个通用的用法脚本,从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)