1
votes

unfortunately I couldn't find a solution that worked for me in other threads.

I am trying to run a vb script using task scheduler via a batch file, both the vbs and the bat file work just fine when I start them manually, but not in Task Scheduler. I tried multiple combinations (I also tried to start the vbs directly) but nothing seems to work. Both .bat and .vbs are directly in C:\

VBS file:

Set WshShell = WScript.CreateObject("WScript.Shell") 
Set Word = CreateObject("Word.Application") 
Set Tasks = Word.Tasks 
For Each Task in Tasks 
  If Task.Visible Then 
    if inStr(Task.name, "(Formal module) - DOORS") then 
      WshShell.AppActivate Task.name 
      WshShell.SendKeys "^s" 
      WScript.Sleep 2000 
      WshShell.SendKeys "%e" 
      WScript.Sleep 200 
      WshShell.SendKeys "e" 
      WScript.Sleep 200 
      WshShell.SendKeys "r" 
      WScript.Sleep 200 
      WshShell.SendKeys "%e" 
      WScript.Sleep 100 
      WshShell.SendKeys "e" 
      WScript.Sleep 100 
      WshShell.SendKeys "r" 
      WScript.Sleep 100 
    Else 
    End If 
  End If 
Next 
Word.Quit 
Set Word = CreateObject("Word.Application") 
Set Tasks = Word.Tasks 
For Each Task in Tasks 
  If Task.Visible Then 
    if inStr(Task.name, "(Formal module) - DOORS") then 
      WshShell.AppActivate Task.name 
      WshShell.SendKeys "^s" 
      WScript.Sleep 2000 
      WshShell.SendKeys "%e" 
      WScript.Sleep 200 
      WshShell.SendKeys "e" 
      WScript.Sleep 200 
      WshShell.SendKeys "r" 
      WScript.Sleep 200 
      WshShell.SendKeys "%e" 
      WScript.Sleep 100 
      WshShell.SendKeys "e" 
      WScript.Sleep 100 
      WshShell.SendKeys "r" 
      WScript.Sleep 100 
    Else 
    End If 
  End If 
Next 
Word.Quit 

batch file:

%windir%\SysWoW64\cmd.exe /C "c:\windows\system32\cscript.exe //B //nologo C:\unlock_doors.vbs"

Scheduled Tesk export:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2017-04-04T12:38:00.000</Date>
    <Author>CCANET\tczimmer</Author>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2017-04-04T00:00:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>CCANET\tczimmer</UserId>
      <LogonType>Password</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
    <Priority>7</Priority>
    <RestartOnFailure>
      <Interval>PT5M</Interval>
      <Count>2</Count>
    </RestartOnFailure>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>C:\unlock_doors.bat</Command>
      <WorkingDirectory>C:\</WorkingDirectory>
    </Exec>
  </Actions>
</Task>
1
You're running the scheduled task in the background? If so I'm pretty certain that SendKeys is not going to work, since it sends the keystrokes to the active (foreground) window.Ansgar Wiechers

1 Answers

0
votes

I had a similar problem where the task scheduler just would not execute my vbs. What I did was create a seperate "timer.vbs" file that had an infinite loop checking what time it was when it reached 6:00 AM it would execute my script. So you have a couple of options here, but in your case to make it so that you wouldn't have to re-write any code, you can probably create a timer.vbs file, still in the c:\ root as such

 'timer.vbs
 Set objShell = Wscript.CreateObject("Wscript.Shell")

 Do
      Wscript.Sleep 1000
      If Time() = TimeValue("12:00:01 AM") Then 'Or modify to the time you need
           objShell.Run "c:\windows\system32\cscript.exe /B /nologo C:\unlock_doors.vbs"
      End If
 Loop

As long as this script is running on a turned on host it should execute at the TimeValue specified.

So basically open a cmd prompt and run cscript timer.vbs within that command prompt and minimize that ommand prompt and go about your normal operations with that host.