Discussion:
Open network connection (VPN) with vbscript
(too old to reply)
grizzly
2004-07-30 23:57:12 UTC
Permalink
I'm trying to open a VPN connection under network connections in WinX
with VBScript. Similar to going to Network properties and doubl
clicking on the VPN connection to bring up the window. The reason fo
this is so the script can run a bunch of prep tasks prior to openin
the VPN connection for the user to connect, but rather than have th
user run the script then open the connection, it would be cleaner to d
it all at once. Any ideas? Thanks


-
grizzl
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------
Alex K. Angelopoulos [MVP]
2004-08-01 02:20:24 UTC
Permalink
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]
2004-08-01 18:05:08 UTC
Permalink
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
2023-05-18 04:04:23 UTC
Permalink
Post by Alex K. Angelopoulos [MVP]
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
SHIB
KAMRAN MANKI
2023-05-18 04:04:48 UTC
Permalink
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
------------------------------------------------------------------------
Loading...