Scheduled task with ADODB

Author Message
TaterTot

  • Total Posts : 33
  • Scores: 0
  • Reward points : 0
  • Joined: 7/26/2010
  • Status: offline
Scheduled task with ADODB Wednesday, February 01, 2012 3:45 AM (permalink)
0
[Helpful answer received] / [List Solutions Only]
Hey Everybody,
I created a script that scans the network for computernames & service tags and stores them in a database (.mdb) file.  The script works great.
 
The problem I'm having is when i schedule the script to run as a scheduled task the script hangs and I don't know where or why.
 
I configured the scheduled task to run as the domain administrator so I know the permissions are good.  I can right-click the task and run it "on demand" and the script runs fine. It seems to only fail when it runs on a schedule and no one is logged in (even though I configured the task to run whether the domain administrator is logged in or not).
 
To add to my confusion, I rewrote the script to use a bunch of text files (using Scripting.FileSystemObject) instead of a database the scheduled task runs fine...
 
With that being said, it would seem the database part is what's causing the trouble. Im using ADODB.Connection and ADODB.Recordset to access the database.
 
I can post code if need be.  Has anyone run into a similar issue with scheduling a script that uses a database?
 
Thanks.
 
#1
    59cobalt

    • Total Posts : 979
    • Scores: 91
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:Scheduled task with ADODB Wednesday, February 01, 2012 7:23 AM (permalink)
    0
    [This post was marked as helpful]
    Hello, and welcome to the royal PITA that is debugging scheduled tasks.

    What Windows version are you running this on, and what are the exact user settings of the task?

    Add some debug logging to your script to identify the exact point where the script doesn't work. Write the log either to a file or to the eventlog. The latter is probably the easier way, since you don't have to deal with file permissions.
    Const EVT_INFORMATION = 4
    Const EVT_SUCCESS     = 0
    
    Set sh = CreateObject("WScript.Shell")
    
    sh.LogEvent EVT_INFORMATION, "Starting"
    ' do some
    sh.LogEvent EVT_SUCCESS, "Marker 1"
    ' do other
    sh.LogEvent EVT_SUCCESS, "Marker 2"
    ' do more
    sh.LogEvent EVT_INFORMATION, "Complete"
    To avoid unnecessary logging during normal execution I usually add a debugging switch:
    debugEnabled = WScript.Arguments.Named.Exists("d")
    '...
    If debugEnabled Then sh.LogEvent EVT_SUCCESS, "..."
    Debug logging is enabled when you run the script with the argument "/d" (without the double quotes).

    Try using my CLogger class to simplify the logging:
    Set logger = New CLogger
    logger.LogToEventlog = True
    logger.LogToConsole  = False
    logger.Debug = WScript.Arguments.Named.Exists("d")
    
    logger.LogInfo "Starting"
    ' do some
    logger.LogDebug "Marker 1"
    ' do other
    logger.LogDebug "Marker 1"
    ' do more
    logger.LogInfo "Complete"

     
    #2
      TaterTot

      • Total Posts : 33
      • Scores: 0
      • Reward points : 0
      • Joined: 7/26/2010
      • Status: offline
      Re:Scheduled task with ADODB Wednesday, February 01, 2012 8:06 AM (permalink)
      0
      Hi 59cobalt,
      Thanks for the help, I appreciate it.
      1. Windows Server 2008 R2 and/or Windows 7 x64.  I have the same issue on both.  With that said, I am aware that I need to run wscript.exe in SysWOW64 to have ADODB run properly.
      Because of this, the scheduled task is actually running a script that runs the main script. It basically does this:
      objShell.Run "C:\Windows\SysWow64\wscript " & WorkingDirectory & "\TakeInventory.vbs"
      I have also tried configuring the scheduled task to run "C:\Windows\SysWOW64\wscript.exe" with the path to my script in the arguments section...still no luck.

      2. I have logged the progress of the script using the event viewer like you have shown. (Thanks by the way, never even considered using the event log)  The last event that is logged is "02 - About to open ConnectionString" (see code below) which makes it very obvious where the disconnect occurs.  Additionally, an .ldf is never created which also indicates the database is never opened.
      -------------CODE:--------------
      Set objShell = CreateObject("WScript.Shell")
      Const EVT_INFORMATION = 4
      Const EVT_SUCCESS     = 0

      Set objCon = CreateObject("ADODB.Connection")
      Set objRec = CreateObject("ADODB.Recordset")
       
      objShell.LogEvent EVT_INFORMATION, "01 - Starting Script declarations"
      dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
      dbSource = "Data Source=AssetInventory.mdb;"
      ConnectionString = dbProvider & dbSource
       
      objShell.LogEvent EVT_SUCCESS, "02 - About to open ConnectionString"
      objCon.Open ConnectionString

      objShell.LogEvent EVT_SUCCESS, "03 - ConnectionString Open"
      MyQuery = "SELECT * FROM Config"

      objShell.LogEvent EVT_SUCCESS, "04 - About to open RecordSet and execute query"
      objRec.Open objCon.Execute(MyQuery)

      objShell.LogEvent EVT_SUCCESS, "05 - ConnectionString and RecordSet open"
       
      While Not objRec.EOF
       Subnet = objRec("Subnet")
       StartIP = objRec("StartIP")
       EndIP = objRec("EndIP")
       objRec.MoveNext 
      WEnd
      objRec.Close
      objCon.Close
      <message edited by TaterTot on Wednesday, February 01, 2012 8:14 AM>
       
      #3
        59cobalt

        • Total Posts : 979
        • Scores: 91
        • Reward points : 0
        • Joined: 7/17/2011
        • Status: offline
        Re:Scheduled task with ADODB Wednesday, February 01, 2012 11:23 AM (permalink)
        0
        [This post was marked as helpful]
        The next step would be to add some error handling at that part of the code that seems to be causing the issue:
        objShell.LogEvent EVT_SUCCESS, "02 - About to open ConnectionString"
        On Error Resume Next
        objCon.Open ConnectionString
        If Err.Number <> 0 Then
         sh.LogEvent 1, Err.Description & " (0x" & Hex(Err.Number) & ")"
         WScript.Quit 1
        Else
         sh.LogEvent EVT_INFORMATION, "Connection state: " & objCon.State
        End If
        On Error Goto 0
        
        objShell.LogEvent EVT_SUCCESS, "03 - ConnectionString Open"

        Did you check the "Don't store password" (your connection seems to be local) and "Run with highest privileges" checkboxes?
        <message edited by 59cobalt on Wednesday, February 01, 2012 11:25 AM>
         
        #4
          TaterTot

          • Total Posts : 33
          • Scores: 0
          • Reward points : 0
          • Joined: 7/26/2010
          • Status: offline
          Re:Scheduled task with ADODB Friday, February 03, 2012 1:28 AM (permalink)
          0
          Hi 59cobalt,
          Thanks again for your help.
           
          After adding the additional event logging I received this error:
          Could not find file 'C:\Windows\system32\AssetInventory.mdb'. (0x80004005)
           
          When you think about it, it makes sense.  Since the "taskschd.exe" is located in the system32 folder, anything in a script with an assumed relative path is going to be looked for in the system32 folder.  In my case, the assumed relative path that was failing was in the dbSource variable:
           
          dbSource = "Data Source=AssetInventory.mdb;"
           
          All I did to fix the issue was create a variable to hold the valid relative path information and add it to my
          dbSource variable:
           
          aScriptFilename = Split(Wscript.ScriptFullName, "\")
          sScriptFilename = aScriptFileName(Ubound(aScriptFilename))
          WorkingDirectory = Replace(Wscript.ScriptFullName, sScriptFilename, "")
          dbSource = "Data Source=" & WorkingDirectory & "\AssetInventory.mdb;"
           
          Below is my finalized code for accessing a database file via task scheduler:
          ----------code------------
          Set objCon = CreateObject("ADODB.Connection")
          Set objRec = CreateObject("ADODB.Recordset")
           
          aScriptFilename = Split(Wscript.ScriptFullName, "\")
          sScriptFilename = aScriptFileName(Ubound(aScriptFilename))
          WorkingDirectory = Replace(Wscript.ScriptFullName, sScriptFilename, "")
           
          dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
          dbSource = "Data Source=" & WorkingDirectory & "\AssetInventory.mdb;"
          ConnectionString = dbProvider & dbSource
           
          objCon.Open ConnectionString
          MyQuery = "SELECT * FROM Config"
          objRec.Open objCon.Execute(MyQuery)
           
          While Not objRec.EOF
           Subnet = objRec("Subnet")
           StartIP = objRec("StartIP")
           EndIP = objRec("EndIP")
           objRec.MoveNext 
          WEnd
           
          objRec.Close
          objCon.Close
          ----------code------------
          Thanks again to 59cobalt for teaching me about the event viewer, logging, and the royal PITA that is debugging.
          scheduled tasks.
           
          #5
            59cobalt

            • Total Posts : 979
            • Scores: 91
            • Reward points : 0
            • Joined: 7/17/2011
            • Status: offline
            Re:Scheduled task with ADODB Saturday, February 04, 2012 10:30 AM (permalink)
            0
            You're welcome.
             
            #6

              Online Bookmarks Sharing: Share/Bookmark

              Jump to:

              Current active users

              There are 0 members and 1 guests.

              Icon Legend and Permission

              • New Messages
              • No New Messages
              • Hot Topic w/ New Messages
              • Hot Topic w/o New Messages
              • Locked w/ New Messages
              • Locked w/o New Messages
              • Read Message
              • Post New Thread
              • Reply to message
              • Post New Poll
              • Submit Vote
              • Post reward post
              • Delete my own posts
              • Delete my own threads
              • Rate post

              2000-2012 ASPPlayground.NET Forum Version 3.9