Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
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.
 
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.
 
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.
 
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.
 
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
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.
 
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.
 
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
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.
 
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
Cannot use parentheses while calling a sub [VBScript compilation error]

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
 
Try w/o parentheses: OWS.Run "..."
 
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.
 
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

 
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("[b]Retrieve accounts from HLP, are you sure?",,"Retrieve Accounts from HLP"[/b],36) 

If intButton <> 6 Then
    WScript.Quit
End if

' Display Msg
InitIE "[b]Connecting to HLP, please wait...[/b]"

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

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

' Update Msg
MsgIE "[b]Mapping network drive, please wait...[/b]"

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

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

'Pause script 3 sec
WScript.Sleep 3000

' Update Msg
MsgIE "[b]Transfering SAGE file, please wait...[/b]"

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

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

' Update Msg
MsgIE "[b]Disconnecting network drive, please wait...[/b]"

' Drop network drive
objNetwork.RemoveNetworkDrive [b]"H:"[/b]

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

'Pause script 3 sec
WScript.Sleep 3000

' Update Msg
MsgIE "[b]Closing connection to HLP, please wait...[/b]"

' Disconnect VPN
OWS.Run "%comspec% /c rasdial [b]HLP[/b] /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 [b]HLP[/b] /DISCONNECT", 0
    'Drop network drive
    objNetwork.RemoveNetworkDrive [b]"H:"[/b]
    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 ;-)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
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
 
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.
 
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?
 
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.
 
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
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top