I want to run a script nightly that will:
- Start VPN
- Perform SQL Query
- Disconnect the VPN
Currently I have a scripts for 1-3 but I have run into an issue scheduling them as a task.
Each script works when I manually run them.
It also works when I schedule a task and set Run Only when user is logged on, and run it manually
But when I select Run whether user is logged on or not nothing happens. The VPN never connects, so the sql can not run, ect..
I have made various changes to the way I reference each script in the task manager (exp using "s, using full path in Program/Script vs using Start In) but nothing seems to work.
Does anyone know what is preventing the task from running?
Here are some screen shots & code:
I'm starting to think that the VPN client is preventing itself from running when I'm not logged on.
I've also written and AutoIt script that will work when I'm logged in but doesn't work when I'm not.
#include <Constants.au3>
;~close any existing vpn
CloseVPN()
;~start cmd
Run("C:\WINDOWS\system32\cmd.exe")
WinWaitActive("C:\WINDOWS\system32\cmd.exe")
;~start vpn
send('"C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe"' & "{ENTER}")
Sleep(5000)
;~disconnect vpn
send("disconnect" & "{ENTER}")
;~send connect commands
send("connect VPNADDRESS" & "{ENTER}")
Sleep(2000)
send("USERNAME" & "{ENTER}")
Sleep(2000)
send("PASSWORD" & "{ENTER}")
Sleep(2000)
;~run and wait for sql command
RunSQL();
;~disconnect vpn
send("disconnect" & "{ENTER}")
CloseVPN()
;~close all cmd processes
Local $process4 = ProcessList("cmd.exe")
For $i = 1 To $process4[0][0]
ProcessClose($process4[$i][1])
Next
;~close cmd window
ProcessClose("C:\WINDOWS\system32\cmd.exe")
WinClose("C:\WINDOWS\system32\cmd.exe")
;~-------------------------------------
;~close vpn function
Func CloseVPN()
Local $process1 = ProcessList("vpncli.exe")
For $i = 1 To $process1[0][0]
ProcessClose($process1[$i][1])
Next
Local $process2 = ProcessList("vpnui.exe")
For $i = 1 To $process2[0][0]
ProcessClose($process2[$i][1])
Next
;~ Local $process3 = ProcessList("vpnagent.exe")
;~ For $i = 1 To $process3[0][0]
;~ ProcessClose($process3[$i][1])
;~ Next
EndFunc
;~-------------------------------------
;~run SQL function
Func RunSQL()
;~start cmd
Local $iPID = Run("C:\WINDOWS\system32\cmd.exe")
WinWaitActive("C:\WINDOWS\system32\cmd.exe")
Send('sqlcmd -Q "EXEC sp_Merge" -S MYSQLSERVER -d MYDATABASE -o "C:\MergeScripts\sql_log.txt"' & "{ENTER}")
Send("exit" & "{ENTER}")
ProcessWaitClose($iPID)
EndFunc