These VBScript functions and code examples have general practicality. Using a cut-and-paste operation, you can place these code examples in your scripts.
The following code automatically inserts the name of the current array object in the input box. You can also type the name of a different array object. If the input box is left empty, it is the equivalent of clicking Cancel.
set root = WScript.CreateObject("FPC.Root") Set collArrays = objFPC.Arrays strArrayName = InputBox("Please enter the array name, or <ENTER> for the containing array:",,root.GetContainingArray.Name) If strArrayName = "" Then 'Cancel option. Note that this should read "Exit Function" if used in a function. Exit Sub Else On Error Resume Next Set objArray = objArrays(strArrayName) If Err.Number <> 0 Then WScript.Echo "The specified array was not found" Exit Sub End If On Error GoTo 0 End If
This function is useful for checking if an object exists before your script adds it. For example, if you try to add a rule called My Rule, and there already is such a rule, this function will allow you to handle that situation, rather than having the script terminate with an error.
Note This function checks for the existence of an item based on the Name property only.
Private Function Item_Exists(oCollection , sItemName) 'This function returns True if the item exists in the given collection, False if it does not exist Dim oItem 'Object Item_Exists = False For Each oItem In oCollection If oItem.Name = sItemName Then Item_Exists = True Exit Function End If Next End Function
The following is an example of how you would call the function. This example adds a new network rule, unless it exists already.
'The next line means - if Item_Exists does not return True, meaning the item does not exist, then... If not Item_Exists(collNetworkRules, "New VPN Network Rule") Then Set oNetworkRule = collNetworkRules.Add("New VPN Network Rule") Else wscript.echo "New VPN Network Rule already exists" wscript.quit End If
This subroutine tells you if an error was encountered during script execution. To use this subroutine, include the following statement at the beginning of the main script.
On Error Resume Next
Then call the subroutine wherever an error is likely, using the following statement.
CheckError
The following is the CheckError subroutine.
Private Sub CheckError() 'Display error information. If Err.Number <> 0 Then WScript.Echo "An error occurred: " & Hex(Err.Number) & " " & Err.Description WScript.Quit End If End Sub
Send comments about this topic to Microsoft
Build date: 11/30/2009
© 2008 Microsoft Corporation. All rights reserved.