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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sheduling VPN connection and data transfer. 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

I've played with DOS Batch, START, RUNDLL etc.. and cannot seem to get the shortcut to run for the VPN connection.

I posted in the windows API and they sugested trying VBScript, a sample code was provided
Code:
Set OWS = CreateObject("Wscript.Shell")
OWS.Run("full_path to your LNK file here")
' i.e. c:\LNKFolder\MyLnk.lnk
but when I try this I get the following error
Invalid procedure call or argument [VBScript runtime error]
Can someone help as to how I can create a file that can be scheduled to acheive my required task.

If I can get a VBS file to work fine, but how would I then issue the net use command and data transfer, would it be the same method i.e. OWS.run "net use", OWS.run "copy [file] [target]" etc... and how would I then kill the VPN connection.

All help is 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.
 
ok I've got my script finished, can you check it over and let me know if you see any major problems, thanks.
Code:
'VBScript to Connect to HLP and retrieve SAGE company accounts'
'For use by authorised personel only'
'Written by 1DMF, Copyright HL Partnership Limited'

Option Explicit

On Error Resume Next

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

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

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

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

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

'Pause script 10 secs
WScript.sleep 10000

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

' Map network drive
objNetwork.MapNetworkDrive "H:", "\\patth\share", False, "USERNAME", "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:\SOURCE c:\PATH\TARGET /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

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 = "Get HLP Accounts Status Window"
      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
Is it possible to put True on the end of all the commands to say wait until complete and then I can remove all the SLEEP commands.

Thanks,
1DMF

"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