2

I want to run a script nightly that will:

  1. Start VPN
  2. Perform SQL Query
  3. 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:

enter image description here enter image description here

enter image description here

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
JonDog
  • 517
  • 7
  • 23
  • 1
    possible duplicate of [Run a batch file with Windows task scheduler](http://stackoverflow.com/questions/4437701/run-a-batch-file-with-windows-task-scheduler) – Alessandro Da Rugna Sep 10 '15 at 12:39
  • Are you trying to connect from a laptop that first needs to set-up a WiFi connection? Because this isn't done by default.. – DarkLite1 Sep 10 '15 at 13:21
  • no. this is from a server with a static ip. – JonDog Sep 10 '15 at 13:29
  • I've tried all of the suggestions Run a batch file with Windows task scheduler post – JonDog Sep 10 '15 at 15:19
  • ya, sorry about that. I tried to paste the code, i tried a screenshot and upload but it kept saying my code was not formatted properly (weird). I ended up having to link to a url. – JonDog Sep 10 '15 at 18:44
  • 4
    Perhaps *AnyConnect* needs data stored in a subdirectory of directory `%APPDATA%` of your user account or in Windows registry under `HKCU` not available on running as scheduled task without being logged in. I suggest to use free [Process Monitor](https://technet.microsoft.com/en-us/sysinternals/bb896645) to log all registry and file system accesses while you run your script from logged in account. Perhaps you can see which data in registry and which files are accessed by *AnyConnect* which might be not available on running the script without being logged in. – Mofi Sep 14 '15 at 17:30
  • What if you tell it to run whether or not a user is logged on and give it credentials to run with. That way it would have a profile to use. – Matt Sep 15 '15 at 00:23
  • Did you try scheduling the jobs directly using powershell commands instead of the built in GUI task scheduler? – gbabu Sep 18 '15 at 11:40

1 Answers1

0

http://www.cisco.com/c/en/us/td/docs/security/vpn_client/anyconnect/anyconnect30/release/notes/anyconnect30rn.html Ctrl+F and type "Open Caveats in Release 3.0.5080"

It looks like there are a lot of problems with that version of AnyConnect that all depend on a lot of things. I'd suggest updating to the latest version. If that version has a bug that applies for you, wait until a version that fixes it is released. Until then, there's not much you can do.

RealCG
  • 53
  • 6