2017-04-04 77 views
1

不幸的是,我找不到在其他线程中为我工作的解决方案。使用批处理文件在任务计划程序中启动vbs脚本

我试图通过批处理文件使用任务计划程序来运行vb脚本,当我手动启动它们时,vbs和bat文件都工作得很好,但不在Task Scheduler中。我尝试了多种组合(我也尝试直接启动vbs),但似乎没有任何工作。 两个.BAT和.vbs直接在C:\

VBS文件:

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 

批处理文件:

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

计划TESK出口:

<?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> 
+0

您正在后台运行计划任务吗?如果是这样,我非常确定'SendKeys'不会起作用,因为它会将键击发送到活动(前台)窗口。 –

回答

0

我有一个类似的问题,任务调度程序不会执行我的vbs。我所做的是创建一个单独的“timer.vbs”文件,该文件具有无限循环,检查它是在何时到达上午6:00它会执行我的脚本。所以你在这里有几个选项,但是为了让你不必重写任何代码,你可以创建一个timer.vbs文件,仍然在c:\ root中,因此

'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 

只要此脚本在打开的主机上运行,​​它应该在指定的TimeValue上执行。

因此,基本上打开cmd提示符并在该命令提示符下运行cscript timer.vbs,并尽量减少该提示并执行与该主机的正常操作。

相关问题