2017-04-04 387 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,並儘量減少該提示並執行與該主機的正常操作。

相關問題