題目很簡單,我想使用指令工具建排程,建立時一併指定啟動引數(Argument)及開始位置:

schtasks.exe 沒提供參數讓你指定引數,但透過 /TR 參數 /tr "'X:\Batch\demo\demo.bat' daily" 可做到類似效果。至於開始位置,schtasks 無法直接設定,只能靠修改 XML 實現。先用 schtasks /tn "排程動作引數測試" /xml > task.xml 將排程設定存成 XML 檔,在其中可找到如下設定:

  <Actions Context="Author">
    <Exec>
      <Command>X:\Batch\demo\demo.bat</Command>
      <Arguments>daily</Arguments>
      <WorkingDirectory>X:\Batch\demo\</WorkingDirectory>
    </Exec>
  </Actions>

修改 WorkingDirectory 後存檔,先刪除排程,再用修改過的 XML 重建排程,即可完成設定變更。

schtasks /delete /tn "排程動作引數測試"
schtasks /create /tn "排程動作引數測試" /xml task.xml

改用 PowerShell 指令建立排程,倒是可透過 New-ScheduledTaskAction -Argument 跟 -WorkingDirectory 直接指定引數跟開始位置,方便許多。但有一好沒兩好,它不支援 Monthly 每月固定某天執行。這回學到用 XML 設定檔建立排程的新做法,應該是最有彈性最無敵的,任何 UI 做得到的奇葩設定都生得出來,未來如再遇疑難雜症,我打算優先用 XML 搞定。


Comments

Be the first to post a comment

Post a comment