次の方法で共有


Importing Lists from Text Files

A list of items that you want to import into a collection can be stored in a text file. Then you can use the Add method of the applicable collection to add the items in the list as elements of the collection.

Adding a List of URLs to a URL Set

If you have a large list of URLs in a text file, you can use a text file to populate a URL set (an FPCURLSet collection) in the FPCURLSets collection for an array. This can be accomplished by reading each line of the text file into a string and calling the Add method of the URL set to add the string as an element of the collection. Then you can apply an access rule represented by an FPCPolicyRule object by adding the URL set to the objects referenced in the URLSets property of the access rule.

This Visual Basic Scripting Edition (VBScript) script creates a new URL set with the name specified by the user if a URL set with that name does not already exist and imports all the URLs listed in the specified text file to the new URL set. If the specified URL set already exists, the URLs in the text file are added without removing the existing URLs. In either case, duplicates are not added. Note that the text file must contain a list of URLs with each URL on a separate line.

Option Explicit
'Define the constants needed
Const Error_FileNotFound = &H80070002
Const ForReading = 1
Main(WScript.Arguments)
Sub Main(args)
    If(args.Count <> 2) Then
        Usage()
    End If
    AddUrlsToUrlSet args(0), args(1)
End Sub
Sub AddUrlsToUrlSet(fileName, urlSetName)
    ' Create the root object.
    Dim root  ' The FPCLib.FPC root object
    Set root = CreateObject("FPC.Root")
    'Declare the other objects needed.
    Dim isaArray        ' An FPCArray object
    Dim urlsets         ' An FPCURLSets collection
    Dim urlset          ' An FPCURLSet object
    Dim fso             ' A FileSystem object
    Dim fileStream      ' A TextStream object
    Dim textRead        ' A string
    ' Get references to the array object 
    ' and the URL sets collection.
    Set isaArray = root.GetContainingArray()
    Set urlsets = isaArray.RuleElements.URLSets
    ' Retrieve the specified URL set.
    On Error Resume Next
    Set urlset = urlsets.Item(urlSetName)
    If Err.Number = Error_FileNotFound Then
        WScript.Echo "The " & urlSetName & " URL set does not exist. Creating it ..."
        Set urlset = urlsets.Add(urlSetName)
    End If
    On Error GoTo 0
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fileStream = fso.OpenTextFile(fileName, ForReading)
    On Error Resume Next
    Do While fileStream.AtEndOfStream <> True
        textRead = fileStream.ReadLine
        If textRead <> "" Then
            Err.Clear
            urlset.Item textRead
            If Err.Number = Error_FileNotFound Then
                WScript.Echo "Adding " &  textRead 
                urlset.Add textRead
            End If
        End If
    Loop
    On Error GoTo 0
    ' Save the changes.
    urlsets.Save
    WScript.Echo "Done!"
End Sub 
Sub Usage()
    WScript.Echo "Usage:" & VbCrLf _
        & "  " & WScript.ScriptName & " FileName UrlSetName" & VbCrLf _
        & "" & VbCrLf _
        & "  FileName - The name of the text file containing the list of URLs" & VbCrLf _
        & "  UrlSetName - The name of the URL set to which the list of URLs will be added" 
    WScript.Quit
End Sub

Assigning a List of Protocols to an Access Rule

You can maintain a list of protocols (names of FPCProtocolDefinition objects) in a text file that can be used to assign the list of protocols to one or more access rules. The protocols in the text file are assigned to an access rule by reading each line of the text file into a string and then calling the Add method of the collection of references (FPCRefs collection) stored in the SpecifiedProtocols property of the access rule to add a reference to the FPCProtocolDefinition object specified by the string to the collection.

This VBScript script removes any protocols assigned to the access rule specified by the user and then adds the protocols listed in the specified text file to the rule. Note that the text file must contain a list of protocols with the name of each protocol on a separate line.

Option Explicit
'Define the constants needed
Const Error_FileNotFound = &H80070002
Const fpcPolicyRuleAccess = 0
Const fpcSpecifiedProtocols = 1
Const ForReading = 1
Const fpcInclude = 0
Main(WScript.Arguments)
Sub Main(args)
    If(args.Count <> 2) Then
        Usage()
    End If
    AddProtocolsToRule args(0), args(1)
End Sub
Sub AddProtocolsToRule(fileName, ruleName)
    ' Create the root object.
    Dim root  ' The FPCLib.FPC root object
    Set root = CreateObject("FPC.Root")
    'Declare the other objects needed.
    Dim isaArray            ' An FPCArray object
    Dim rules               ' An FPCPolicyRules collection
    Dim rule                ' An FPCPolicyRule object
    Dim protocols           ' An FPCProtocolDefinitions collection
    Dim specifiedProtocols  ' An FPCRefs collection
    Dim fso                 ' A FileSystem object
    Dim fileStream          ' A TextStream object
    Dim textRead            ' A String
    Dim I                   ' An Integer
    ' Get references to the array object, the policy rules collection, 
    ' and the protocol definitions collection.
    Set isaArray = root.GetContainingArray()
    Set rules = isaArray.ArrayPolicy.PolicyRules
    Set protocols = isaArray.RuleElements.ProtocolDefinitions
    ' Retrieve the specified policy rule.
    On Error Resume Next
    Set rule = rules(ruleName)
    If err.Number = Error_FileNotFound Then
        WScript.Echo "The access rule " & ruleName & " could not be found."
        WScript.Quit
    End If
    Err.Clear
    On Error GoTo 0
    ' Verify that the specified rule is an access rule.
    If rule.Type <> fpcPolicyRuleAccess Then
        WScript.Echo "The policy rule " & ruleName & " is not an access rule."
        WScript.Quit
    End If
    WScript.Echo "Configuring the rule to apply to a specified set of protocols ..."
    If rule.AccessProperties.ProtocolSelectionMethod <> fpcSpecifiedProtocols Then
        rule.AccessProperties.ProtocolSelectionMethod = fpcSpecifiedProtocols
    End If
    WScript.Echo "Retrieving the collection for storing the specified protocols ..."
    Set specifiedProtocols = rule.AccessProperties.SpecifiedProtocols
    If specifiedProtocols.Count > 0 Then
        WScript.Echo "Removing all protocols from the collection ..."
        specifiedProtocols.RemoveAll
    End If
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fileStream = fso.OpenTextFile(fileName, ForReading)
    Do While fileStream.AtEndOfStream <> True
        textRead = fileStream.ReadLine
        If textRead <> "" Then
        On Error Resume Next
        protocols.Item textRead
            If Err.Number = Error_FileNotFound Then
                WScript.Echo "The " &  textRead & " protocol is not defined in Forefront TMG."
                Err.Clear
            Else
                specifiedProtocols.Add textRead, fpcInclude
            End If
        End If
    Loop
    On Error GoTo 0
    ' Save the changes to the access rule.
    rule.Save
    WScript.Echo "Done!"
End Sub 
Sub Usage()
    WScript.Echo "Usage:" & VbCrLf _
        & "  " & WScript.ScriptName & " FileName RuleName" & VbCrLf _
        & "" & VbCrLf _
        & "  FileName - The name of the text file containing the list of protocols" & VbCrLf _
        & "  RuleName - The name of the access rule to which the list of protocols will be added" 
    WScript.Quit
End Sub

 

 

Build date: 7/12/2010