Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Open network connection (VPN) with vbscript

567 views
Skip to first unread message

grizzly

unread,
Jul 30, 2004, 7:57:12 PM7/30/04
to

I'm trying to open a VPN connection under network connections in WinXP
with VBScript. Similar to going to Network properties and double
clicking on the VPN connection to bring up the window. The reason for
this is so the script can run a bunch of prep tasks prior to opening
the VPN connection for the user to connect, but rather than have the
user run the script then open the connection, it would be cleaner to do
it all at once. Any ideas? Thanks.

--
grizzly
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Alex K. Angelopoulos [MVP]

unread,
Jul 31, 2004, 10:20:24 PM7/31/04
to
grizzly wrote:
> I'm trying to open a VPN connection under network connections in WinXP
> with VBScript. Similar to going to Network properties and double
> clicking on the VPN connection to bring up the window. The reason for
> this is so the script can run a bunch of prep tasks prior to opening
> the VPN connection for the user to connect, but rather than have the
> user run the script then open the connection, it would be cleaner to do
> it all at once. Any ideas? Thanks.

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


Alex K. Angelopoulos [MVP]

unread,
Aug 1, 2004, 2:05:08 PM8/1/04
to
Alex K. Angelopoulos [MVP] wrote:
> grizzly wrote:
>> I'm trying to open a VPN connection under network connections in WinXP
>> with VBScript.

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


KAMRAN MANKI

unread,
May 18, 2023, 12:04:25 AM5/18/23
to
SHIB

KAMRAN MANKI

unread,
May 18, 2023, 12:04:50 AM5/18/23
to
0 new messages