Search code examples
vbscripthta

HTA vbs script cannot execute program when scheduled task launched


Trying to make a simple kiosk HTA, that autostarts when the user log on. But I cannot get it to work, when I add the hta as a scheduled task.

I use a hta file as a splash screen, while it executes needed scripts and programs and then the kiosk interface HTA file.

When I run the splash hta manually, everything works as intended. But when I add the splash HTA as a scheduled task it breaks my execute script and can't find any of the programs and scripts I tell it to launch.

I use Inno setup to install the kiosk and also want it the setup the scheduled task if possible.

The files I need to run are installed to: C:\Users\USER\AppData\Local\Programs\MyKiosk\data

The scheduled task action is: Program: C:\Windows\System32\mshta.exe Arguments: %localappdata%\Programs\Mykiosk\Splash.hta

This is the vbs script from the splash HTA:

<HTA:APPLICATION
    CONTEXTMENU = "no"
    BORDER="none"
    INNERBORDER = "no"
    SINGLEINSTANCE = "yes"
    SHOWINTASKBAR = "no"
    SCROLL="no"/>

 <script type="text/vbscript">

    Sub window_onload()
        CenterWindow (screen.Width - widthX)/4, (screen.Height - heightY)/4.5

        Call Execute("program.exe")
        Call Execute("script.bat")
        Call Sleep(1)
        Call Execute("StartKiosk.bat")
        Window.Close

    End Sub

    '-----------------------------WindowsSize-----------------------------------------
    Sub CenterWindow( widthX, heightY )
        self.ResizeTo widthX, heightY 
        self.MoveTo (screen.Width - widthX)/2, (screen.Height - heightY)/2
    End Sub
        '----------------------------Execute---------------------------------------
    Sub Execute(Program)
        set shell=CreateObject("Shell.Application")
        ' shell.ShellExecute "application", "arguments", "path", "verb", window
        shell.ShellExecute ""&Program&"",,"data\", "runas", 0
        set shell=nothing
    End sub
    '-----------------------------Sleep-----------------------------------------
    Sub Sleep(seconds)
        CreateObject("WScript.Shell").Run "%COMSPEC% /c ping 127.0.0.1 -n " _
        & seconds+1, 0, True
    End Sub

    </script>

I've tried replacing the data\ path to %localappdata%\Programs\Mykiosk\data\ But that didn't help, get the same Windows cannot find the program errors.

I'm stuck, can anyone tell me what I'm doing wrong?

Update:

Looks like a problem with the "Start in" parameter is missing..

So can anyone tell my how I would setup the scheduled task with using a SCHTASKS command?

The instruction page don't make it really clear, how to add working dir and do ONLOGON with a specific user(user running installer)..

Here is my attempt to create the scheduled task with Inno Setup:

Filename: "schtasks"; \
    Parameters: "/Create /F /SC ONLOGON /TN ""My Kisok"" /TR ""'C:\Windows\System32\mshta.exe' {app}\Splash.hta"""; \
    Flags: runhidden

Also how do I make ONLOGON with a specific user(user running installer)

I tried addig /U {username} but that breaks the script.


Solution

  • Try to create a task with a vbscript : HTA_Tasker_Creator.vbs

    Option Explicit
    Dim Ws,TaskName,Repeat_Task,HTA_FILE_PATH
    Set Ws = CreateObject("Wscript.Shell")
    TaskName = "Open_HTA"
    Repeat_Task = 30 ' in minutes
    HTA_FILE_PATH = "%localappdata%\Programs\Mykiosk\data\Splash.hta"
    Ws.run HTA_FILE_PATH,1,True
    Call Create_Schedule_Task(TaskName,Repeat_Task)
    '----------------------------------------------------------------------
    Sub Create_Schedule_Task(TaskName,Repeat_Task)
    Dim Ws,Task,Result
    Set Ws = CreateObject("Wscript.Shell")
    Task = "CMD /C Schtasks /Create /SC DAILY /ST 08:00 /F /RI "&_
    Repeat_Task &" /DU 24:00 /TN "& TaskName &" /TR "& WScript.ScriptFullName &""
    Result = Ws.run(Task,0,True)
    End Sub
    '----------------------------------------------------------------------
    

    Edit : 24/08/2020 @ 00:52

    Here is another vbscript that i added some functions to create a task ONLOGON and it can be deleted too with admin rights !

    Option Explicit
    Dim Ws,TaskName,Repeat_Task,HTA_FILE_PATH
    Set Ws = CreateObject("Wscript.Shell")
    Call Run_as_Admin() ' We execute our script with admin rights !
    TaskName = "My Kisok"
    Repeat_Task = 30 ' in minutes
    HTA_FILE_PATH = "%localappdata%\Programs\Mykiosk\data\Splash.hta"
    Ws.run HTA_FILE_PATH,1,True
    Call Create_Schedule_Task_ONLOGON(TaskName)
    'Call Create_Schedule_Task(TaskName,Repeat_Task)
    'Call Delete(TaskName) 'If You want to delete the Task just uncomment this line and comment the subroutine with Create_Shedule... in name
    '-------------------------------------------------------------------------------------
    Sub Create_Schedule_Task(TaskName,Repeat_Task)
    Dim Ws,Task,Result
    Set Ws = CreateObject("Wscript.Shell")
    Task = "CMD /C Schtasks /Create /SC DAILY /ST 08:00 /F /RI "&_
    Repeat_Task &" /DU 24:00 /TN "& DblQuote(TaskName) &" /TR "& WScript.ScriptFullName &""
    Result = Ws.run(Task,0,True)
    End Sub
    '-------------------------------------------------------------------------------------
    Sub Create_Schedule_Task_ONLOGON(TaskName)
    Dim Ws,Task,Result
    Set Ws = CreateObject("Wscript.Shell")
    Task = "CMD /C Schtasks /Create /SC ONLOGON /F "&_
    "/TN "& DblQuote(TaskName) &" /TR "& WScript.ScriptFullName &""
    Result = Ws.run(Task,0,True)
    End Sub
    '-------------------------------------------------------------------------------------
    Sub Run_as_Admin()
    If Not WScript.Arguments.Named.Exists("elevate") Then
       CreateObject("Shell.Application").ShellExecute DblQuote(WScript.FullName) _
       , DblQuote(WScript.ScriptFullName) & " /elevate", "", "runas", 1
        WScript.Quit
    End If
    End Sub
    '-------------------------------------------------------------------------------------
    Function DblQuote(Str)
        DblQuote = Chr(34) & Str & Chr(34)
    End Function
    '-------------------------------------------------------------------------------------
    Sub Delete(TaskName)
    Dim Ws,Task,Result
    Set Ws = CreateObject("Wscript.Shell")
    Task = "CMD /C schtasks /Delete /TN "& DblQuote(TaskName) &" /F"
    Result = Ws.run(Task,0,True)
    End Sub
    '-------------------------------------------------------------------------------------