Compiled Script works when called directly but fails as a scheduled task

Started by MrPenska, March 29, 2015, 04:48:33 PM

Previous topic - Next topic

MrPenska

I have been running this stock code for years.  It creates a static htm file from an asp file.  I ported it over to a Windows 2012 Server.  It works when called directly on the server, however, when running as a scheduled task that I created while logged in as the user, the file myfile.htm is never created.  When it works, the file myfile.htm is created in the directory that the Winbatch Executable is called from.

I put a tracefile in the script, and ran it by itself and from the task.  I donââ,¬â,,¢t see any complaints in the trace file when the executable failed to create the htm, and the other trace file outputs are identical.

The executable works as a scheduled task on a Windows 2003 server, BUT the logged in user is a domain admin, where the logged in user on the Windows 2012 Server is not a domain admin, however, it does have the right to create files in the directory that the script is called from and the htm file gets to.

What do I need to do to get this working from the task scheduler?

The last step from the tracefile when called directly:

FilePut(filename, bodytxt)
(121406) VALUE=> 31316

The last step from the tracefile when called from a task:

FilePut(filename, bodytxt)
(121312) VALUE=> 31316

Code:

tracefile="C:\temp\trace.txt"

if FileExist(tracefile)

   FileDelete(tracefile)


endif
DebugTrace(@ON,tracefile)

                                                                   ;   here's the basic download using OLE.
;
;   I'll leave you to write the WinInet Code to do the actual image downloads,
;   and make any changes...it's interactive to start, then you can modify it
;   to make it "auto-magic" ;)

;   Winbatch 2004F, MSIE 6

#DefineSubRoutine startMSIE(url)
   browser = ObjectOpen("InternetExplorer.Application")
   browser.addressbar = @FALSE
   browser.statusbar = @FALSE
   browser.menubar = @FALSE
   browser.toolbar = @FALSE
   browser.visible = @FALSE
   browser.navigate(url)
   ;   wait until page loads...
   WaitForPageLoad()
   ;   setup the document object...
   browserDoc = browser.Document
   all = browserdoc.all
   Return(browser)
#EndSubRoutine

#DefineSubroutine WaitForPageLoad()  ; assume Browser
   While browser.busy || browser.readystate == 1
      TimeDelay(60.5)
   EndWhile
   ;While browser.Document.ReadyState != "complete"
      TimeDelay(60.5)
;   EndWhile
   return
#EndSubroutine



;   load WinInet extender
AddExtender("wwint34i.dll")

;   URL of page you wish to Download...
;url = "C:\Test\Winbatch Main Page.htm"     
url = "http://myurl/us.asp"               ;<--- modify to the page you need...
;
;   specify the folder you wish to store the pages in...
DownloadFolder = "f:\automation"            ;<--- modify to your own location...
;
;   check for trailing backslash
if StrSub(DownloadFolder,StrLen(DownloadFolder),1) != "\" then DownloadFolder = StrCat(DownloadFolder,"\")
;
;   start the browser...
br = startMSIE(url)
;
;   build a current filename from the .title of the current page...
cfname = StrTrim(browserdoc.title)
If StrSub(cfname, StrLen(cfname), 1) == "."
   cfname = StrCat(DownloadFolder, cfname, "htm")
Else
   cfname = StrCat(DownloadFolder, cfname, ".htm")
EndIf
;   double check with the user...
filename =("myfile.htm")
;filename = AskLine("Debug", "SAVE AS FILENAME", cfname)
;
;  build an image folder inside the download folder...
imageFolder = StrCat(DownloadFolder, FileRoot(filename),"_Files\")
;   make the new folder...
DirMake(imageFolder)
;
;   open WinInet session
tophandle=iBegin(0,"","")

;
;   loop thru the images on the page and grab...
ImageList = browserdoc.GetElementsBytagName("IMG")
For x = 0 To ImageList.length-1
   thisImage = ImageList.item(x)
   ;   the source image...
   thisSRC   = thisImage.SRC
   ;   only the filename for the destination filename...
   thisIMG = ItemExtract(ItemCount(thisSRC, "/"), thisSRC, "/")
   ;   download image file
   datahandle=iUrlOpen(tophandle,thisSRC)
   xx=iReadData(datahandle,StrCat(imageFolder,thisIMG))
   ;   specify relative image path
   thisImage.SRC = StrCat(FileRoot(filename),"_Files\", thisIMG)
   ;   close object
   ObjectClose(thisImage)
Next

;   loop thru the background images on the page and grab...
BackImageList = browserdoc.GetElementsBytagName("table")
For y = 0 To BackImageList.length-1
   thisBackImage = BackImageList.item(y)
   ;   the source image...
   thisBackSRC   = thisBackImage.background
   if thisBackSRC=="" then continue
   ;   only the filename for the destination filename...
   thisBackIMG = ItemExtract(ItemCount(thisBackSRC, "/"), thisBackSRC, "/")
   ;   download image file
   datahandle=iUrlOpen(tophandle,thisBackSRC)
   xx=iReadData(datahandle,StrCat(imageFolder,thisBackIMG))
   bck = StrCat(FileRoot(filename),"_Files\", thisBackIMG)
   ;   specify relative image path
   thisBackImage.background = bck
   ;   close object
   ObjectClose(thisBackImage)
Next

;   close WinInet session
;iClose(datahandle)
iClose(tophandle)


;   now grab the HTML from the page...
body = browserdoc.body
bodytxt = body.outerHTML

;   strcat a comment to the top of it, with the URL of the page just in case
;   you need it later...
bodytxt = StrCat("", @CRLF, bodytxt, "")
;
;   write the new HTML to the file...
FilePut(filename, bodytxt)
;
;Message("Debug", "All Done")

;Return
timedelay(60)
Run("randombatchfile", "")
Exit




td

Try turning on Task Scheduler history, run your task, and check the history log.  If you search this forum you will find several other posts regarding this topic.  Some of the usual suspects are not setting 'Run with highest privilege' or the 'Run only when user is logged on' options.   
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

MrPenska

The task history isn't helpful.  It simply states that the task ran successfully.  I'm not using winhide, so I can tell that the job is running and can tell that it executes some time delays and runs an external batch file that it calls.  It just doesn't produce the htm file.

I am running it with the 'Run only when user is logged on' setting, and NOT with the  'Run with highest privilege'  setting.  If I change any of the settings, the job states that it kicks off, but literally does nothing and
quits instantly with task scheduler erroneously reporting that the task completed.

I'll have to check similar posts to see if there are any other suggestions.

td

Actually, the history file is helpful because the fact that the task runs to completion eliminates several possible causes of the problem.  If you are using an eleven year old version of WinBatch, you are using an un-manifested executable and that may be the underlying cause of your problem.  You may also wish to search for your html file in other locations on you system.  File system visualization can sometimes be a side-effect of using un-manifested executables on UAC enabled systems.

Please keep in mind that the above possible explanations are suggestions to aid in your investigation and not assertions of cause.   
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

Some of the file system location that can be vitalized on UAC enabled systems include


  • \Program Files and subfolders
  • \Program Files (x86) on 64-bit systems
  • \Windows and all subfolders, including System32
  • \Users\%AllUsersProfile%\ProgramData
  • \Documents and Settings



"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

MrPenska

OK - Winbatch support comes through once again.  I found the htm file in:

C:\Users\username\AppData\Local\VirtualStore\Windows\SysWOW64

A quick Google search shows that Windows puts files there if it doesn't have permission to put them somewhere else.

UAC is an major pain.  There are no issues creating the htm file to a network drive when I ran the Winbhatch executable from a command prompt or explorer window, but when I run it as a task, Windows does not think it has permissions to write to the drive, so it redirects it locally, while fooling the tracefile to output as if it created the file where it was intended to be created.

As a workaround, in this last step:

Run("randombatchfile", "") - My randombatchfile copies the htm file from C:\Users\username\AppData\Local\VirtualStore\Windows\SysWOW64 to the network share.  Windows has no problems with this step, when running as part of the Winbatch executable, while running from a scheduled task.

My take on this is 2 things:

1 - Windows UAC Security seems to think that tasks are more dangerous than manual execution of programs.

2 - Windows UAC security thinks that executable files are more dangerous than batch files.

Thanks for your help.  I think that eventually I would have looked for the file on the local drive, but your suggestion saved me many hours of additional troubleshooting. :)