×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How do i start a shortcut or run a windows network connection

How do i start a shortcut or run a windows network connection

How do i start a shortcut or run a windows network connection

(OP)
Hello,

I've been tasked with the job to write a batchfile that can be put into the windows scheduled tasks to do the following...

1. Launch the VPN decktop icon
2. Issue the map network drive command (net use)
3. Copy a file across the network
4. Disconnect the VPN

2 + 3 is not a problem, I can work that one out, however i've searched and searched google and cannot find a way to launch a shortcut, I found how to create them but not launch them

I've tried the following also, with no joy
rundll32.exe url.dll,FileProtocolHandler c:\path\mylink.lnk

plus lookled up all the SHELL32.dll comands but can't seem to find what I need

How do I auto-launch a desktop shortcut / .lnk file

Also how would I disconnect the VPN connection, is there a command line program to kill a process?, would that be the way to do it.

and finally how do I tell a batch job to SLEEP x seconds, I know there is PAUSE, but that would require human response to carry on.

I've tried the START command but that doesn't launch the shortcut.

Any help with a way to acheive this would be very much appreciated.

Regards,

1DMF.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

RE: How do i start a shortcut or run a windows network connection

How about ShellExecute?
Or instead of API to use Windows Scripting Host object WSCRIPT.Shell has Run() method.

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.

RE: How do i start a shortcut or run a windows network connection

(OP)
sounds great if it will work, what are they? and how do i use them ?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

RE: How do i start a shortcut or run a windows network connection

To use ShellExecute you must write an application, becuase this is an API function. Youcan't directly use it in batch file. To use Windows Scripting host you must write a small file with extention VBS (Visual Basic Script) and run it in Scheduler. Here an example opf that file
Save this code in Test.VBS and than double click on it.

CODE

Set OWS = CreateObject("Wscript.Shell")
OWS.Run("full_path to your LNK file here")
' i.e. c:\LNKFolder\MyLnk.lnk

Maybe you must look in VBScript forum for more help, because I mostly used scripting in my FrontEnd and never write a vbs files.

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.

RE: How do i start a shortcut or run a windows network connection

(OP)
ahh , I see the problem, I don't have VB Studio, so cannot write VB code and compile.

I have MS Office so could possibly do this with VBA using MS Access and compile to MDE executable.

I'll post in the VBScript forum as i'm getting an error with your example

Quote:

Invalid procedure call or argument [VBScript runtime error]

Thanks for the advice.
1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

RE: How do i start a shortcut or run a windows network connection

To create a VBS file you need only notepad, no compilation needed.
About Error, strange it works for me w/o any errors
WinXP SP2?
try to change this:
OWS.Run("full_path to your LNK file here")
to
OWS.Run("full_path to your LNK file here", 1, 0)

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.

RE: How do i start a shortcut or run a windows network connection

(OP)
OK, I've tried that - here is the exact code used

CODE

Set OWS = CreateObject("Wscript.Shell")
OWS.Run("c:\HLPVPN\HLP.lnk",1,0)
I now get the following error

Quote:

Cannot use parentheses while calling a sub [VBScript compilation error]
any ideas?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

RE: How do i start a shortcut or run a windows network connection

I just create one folder named HLPVPN and copy and paste one of my Desktop LNK to it and renamed it to HLP.LNK. Here what works here:

CODE

Set OWS = CreateObject("Wscript.Shell")
OWS.Run("c:\HLPVPN\HLP.lnk")
If I put additional parameters I got the same error message as you

Quote:


Cannot use parentheses while calling a sub [VBScript compilation error]

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.

RE: How do i start a shortcut or run a windows network connection

Try w/o parentheses: OWS.Run "..."

RE: How do i start a shortcut or run a windows network connection

(OP)
The VBScript forum has supplied a working line of code

CODE

OWS.Run "%comspec% /k c:\LNKFolder\MyLnk.lnk"

it seems as soon as I added the /k switch BINGO.... now to the next issue...

1. How to I pause a VBS script x seconds to - give it a chance to connect.
2. How do disconnect the VPN process, probaly a kill process command would be easiest, is there one?

plus I've checked Task Manager and when the VPN connects I cannot see a process or Application relating to it.

How do i close the VPN connection after data transfer?

1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

RE: How do i start a shortcut or run a windows network connection

Hello 1DMF.

I was just wondering if you ever got that script written in VBS as I need the exact same thing as you stated in your original request (including the delay while the files copy).  If so could you share that code with us?

Thank you

RE: How do i start a shortcut or run a windows network connection

(OP)
sure thing I got it working a treat, here you go ...

you will need to change the bits highlighted accordingly

CODE

'VBScript to Connect to VPN and retrieve file'
'For use by authorised personel only'
'Written by 1DMF, Copyright HLP'

Option Explicit

On Error Resume Next

' Define vars
Dim OWS, intReturn, objIE, strIETitle, objShell, k, objNetwork, intButton

' define objects
Set OWS = CreateObject("Wscript.Shell")
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")

intButton = OWS.Popup("Retrieve accounts from HLP, are you sure?",,"Retrieve Accounts from HLP",36)

If intButton <> 6 Then
    WScript.Quit
End if

' Display Msg
InitIE "Connecting to HLP, please wait..."

' Run VPN link
OWS.Run "%comspec% /c rasdial HLP", 0, true

If Err.Number <> 0 Then
      Error_Msg
      WScript.Quit
End If

' Update Msg
MsgIE "Mapping network drive, please wait..."

' Map network drive
objNetwork.MapNetworkDrive "H:", "\\file\path", False, "USERID", "PASSWORD"

If Err.Number <> 0 Then
      Error_Msg
      WScript.Quit
End If

'Pause script 3 sec
WScript.Sleep 3000

' Update Msg
MsgIE "Transfering SAGE file, please wait..."

' Transfer File
OWS.Run "%comspec% /c copy h:\remote_filename c:\path\target_filename /Y", 0, true

If Err.Number <> 0 Then
      Error_Msg
      WScript.Quit
End If

' Update Msg
MsgIE "Disconnecting network drive, please wait..."

' Drop network drive
objNetwork.RemoveNetworkDrive "H:"

If Err.Number <> 0 Then
      Error_Msg
      WScript.Quit
End If

'Pause script 3 sec
WScript.Sleep 3000

' Update Msg
MsgIE "Closing connection to HLP, please wait..."

' Disconnect VPN
OWS.Run "%comspec% /c rasdial HLP /DISCONNECT", 0, true

If Err.Number <> 0 Then
      Error_Msg
      WScript.Quit
End If

' Close display box
Wscript.Sleep 3000
MsgIE "IE_Quit"

' Clean Up
Set objIE = Nothing
Set objShell = Nothing
Set OWS = Nothing
Set objNetwork = Nothing

' Display completed msg
Wscript.Echo "Transfer Complete!"

' exit
WScript.Quit

' ################# SUB ROUTINES ##################
Sub MsgIE(strMsg)
' Subroutine to display message in IE box and detect when the
' box is closed by the program or the user.
  On Error Resume Next
  If strMsg = "IE_Quit" Then
    blnFlag = False
    objIE.Quit
  Else
    objIE.Document.Body.InnerText = strMsg
    If Err.Number <> 0 Then
      Err.Clear
      blnFlag = False
      Exit Sub
    End If
    objShell.AppActivate strIETitle
  End If
End Sub

Sub InitIE(strMsg)
' Subroutine to initialize the IE display box.
  Dim intWidth, intHeight, intWidthW, intHeightW
  Set objIE = CreateObject("InternetExplorer.Application")
  With objIE
    .ToolBar = False
    .StatusBar = False
    .Resizable = False
    .Navigate("about:blank")
    Do Until .readyState = 4
      Wscript.Sleep 100
    Loop
    With .document
      With .ParentWindow
        intWidth = .Screen.AvailWidth
        intHeight = .Screen.AvailHeight
        intWidthW = .Screen.AvailWidth * .40
        intHeightW = .Screen.AvailHeight * .05
        .resizeto intWidthW, intHeightW
        .moveto (intWidth - intWidthW)/2, (intHeight - intHeightW)/2
      End With
      .Write "<body> " & strMsg & " </body></html>"
      With .ParentWindow.document.body
        .style.backgroundcolor = "LightBlue"
        .scroll="no"
        .style.Font = "10pt 'Courier New'"
        .style.borderStyle = "outset"
        .style.borderWidth = "4px"
      End With
      .Title = "Retrieve HLP Accounts"
      objIE.Visible = True
      Wscript.Sleep 100
      objShell.AppActivate strIETitle
    End With
  End With
End Sub

Sub Error_Msg()
    MsgIE "There was an error, please close this window and try again!"
    ' Disconnect VPN
    OWS.Run "%comspec% /c rasdial HLP /DISCONNECT", 0
    'Drop network drive
    objNetwork.RemoveNetworkDrive "H:"
    Set objIE = Nothing
    Set objShell = Nothing
    Set OWS = Nothing
    Set objNetwork = Nothing
    WScript.Quit
End Sub

the machine running the code will need to have set up the VPN client program for the rasdial command ours is called 'HLP' , most are just messages need changing for your needs, I used drive letter H: to connect the remote share to the local machine, again your choice.

you will also need to specify the file paths and names along with userid and password of account allowed to connect to domain network drive for mapping.

if you need any help just let me know, good luck winky smile

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

RE: How do i start a shortcut or run a windows network connection

Thank you so much for your script.  Very helpful.

I am having a problem getting the VPN connection established.

In the section

CODE

' Run VPN link
OWS.Run "%comspec% /c rasdial pcf", 0, true
 
Where "pcf" is the name of my VPN connection, if I leave it as you have it in the previous post, it does not make my vpn connection for me even though it continues on and tells me it is "Mapping network drive, please wait..." before giving me the error message "There was an error, please close this window and try again!". If I make the VPN connection manually before running the script it all works properly and then it does not disconnect the VPN connection at the end.

I used the following code as a workaround but the script is not as smart after this as RUN no longer knows when the VPN is connected so I had to make the script Sleep to ensure it has enough time to connect before attempting to map the drive.

CODE

OWS.Run "%comspec% /k c:\pcf.lnk", 0, false
WScript.Sleep 10000
(notice true is changed to false in order to get the script to continue)

Thank you again.

Greg

RE: How do i start a shortcut or run a windows network connection

(OP)
when you open your 'network connections' do you have a VPN connection called 'pcf'.

remember this is done using windows system completely, including the VPN, not a third party VPN client.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

RE: How do i start a shortcut or run a windows network connection

Yes I am using the MS VPN and yes PCF shows in the network connections. I have used pretty much all the defaults and when I double click on PCF it does connect automatically without prompting me for username and password.

What I think is odd is that at the end of the script the following code does work to terminate the VPN connection.

CODE

' Disconnect VPN
OWS.Run "%comspec% /c rasdial pcf /DISCONNECT", 0, true

A separate question: at the end of the script a dialog box comes up that says "Transfer Complete" and the title bar of that dialog box says "Windows Scripting Host".  Is there some way to change the text in the title bar of that dialog box?

RE: How do i start a shortcut or run a windows network connection

I thought I should clarify something from my previous post. I mentioned that it is odd that the vpn does get disconnected and that at the end of the script there is a dialog box that comes up that says "Tranfer Complete".  These two things only happed when I have used my "Workaround" as stated in my post from Sept 18th.

RE: How do i start a shortcut or run a windows network connection

OK I slogged on and figured out that %comspec% means that it is a dos command line program so I went there to troubleshoot. I found out that it wont connect unless I specify the VPN username and password in the VBScript. The connection does run without typing in any username and password if I run it in windows.  I would prefer not to have to enter the UN and PW into the script since it is open in plain text.  Any ideas?  

I noticed that the code you presented above does not make mention of the UN and PW in the VPN connection section.

Thanks again.

Greg

RE: How do i start a shortcut or run a windows network connection

(OP)
ahh. that's because we use SSL certification instead to ensure the VPN tunnel is encrypted, the VPN doesn't use UN & PWD.

if you have entered and saved the UN & PWD into the VPN connection, I'm unsure why it then needs you to pass it one when issuing the rasdial command, maybe you need a switch or attribute to tell it to use current "saved" details.

as for the message, it is a WSH echo command, you may be able to pass it a parrameter for window title, is there a reason for wanting to change it?




"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close