--
grizzly
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------
Something _sort of_ like this will work. It has problems, though...
+ You need to know the exact name of the connection and use it in place of
the "VPN Connection".
+ If your script terminates before the connection is complete, the
connection will also terminate. I've got a sleep time of 35 seconds
inserted for the demo. Obviously, you want to wait for as long as it takes
to ensure you're connected, then begin doing what you need to do.
Set sa = CreateObject("Shell.Application")
targetConnection = LCase("VPN Connection")
set NetConn = sa.Namespace(49)
Set Connections = NetConn.Items
For i = 0 to Connections.Count - 1
If LCase( Connections.Item(i).Name) = targetConnection Then
set cnx = Connections.Item(i)
'WScript.Echo i, cnx.Name, cnx.Path
cnx.Verbs.Item(0).DoIt
For j = 0 to cnx.Verbs.Count - 1
'WScript.Echo "--", j, cnx.Verbs.Item(j).Name
If cnx.Verbs.Item(j).Name = "C&onnect" Then
cnx.Verbs.Item(j).DoIt()
WScript.Sleep 35000
Exit For
End If
Next
Exit For
End If
next
After looking through a few old Bakken & Harris posts I decided to write a
more generic method for handling items that "toggle" and may take time to do
so. Here's a function which can be used for any addressable shell namespace
item; you simply supply it with the namespace index (49 for network
connections), the item name ("VPN Connection" in our case) and the verb
("C&onnect" for English language systems).
Option Explicit
Dim t0: t0 = Timer
WScript.Echo "Success?", _
ToggleShellItem(49, "VPN Connection", "C&onnect")
WScript.Echo timer - t0
Function ToggleShellItem(ByVal namespace, ByVal item, ByVal verb)
' Use a shell folder item verb which disappears after use.
' Examples are Connect/Disconnect, Enable/Disable pairs.
' namespace: numeric ID of the namespace
' itemName: name of the item when viewed in the namespace window.
' verbName: verb to activate.
' The function will find the verb in the item, DoIt, wait
' until the verb "disappears" from the verb list, then return True.
' If the item or verb is not found, returns False.
ToggleShellItem = False
Dim sa: Set sa = CreateObject("Shell.Application")
Dim items, i
Set items = sa.Namespace(namespace).Items
For i = 0 to items.Count - 1
If LCase( items.Item(i).Name) = LCase(item) Then
Dim target: set target = items.Item(i)
Exit For
End If
Next
For i = 0 to target.Verbs.Count - 1
If LCase(target.Verbs.Item(i).Name) = LCase(verb) Then
target.Verbs.Item(i).DoIt
Do While LCase(target.Verbs.Item(i).Name) = LCase(verb)
WScript.Sleep 500
Loop
ToggleShellItem = True
Exit For
End If
Next
End Function