Skip to content

Instantly share code, notes, and snippets.

@rasor
Last active December 20, 2015 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rasor/6208402 to your computer and use it in GitHub Desktop.
Save rasor/6208402 to your computer and use it in GitHub Desktop.
A sample for scheduling a job (myjob.exe) in task scheduler. http://rasor.wordpress.com/2013/08/12/powershell-scheduling-a-task/
c:
cd C:\PathToExecutableFile
PowerShell.exe -ExecutionPolicy Bypass -NonInteractive -FILE myjob.ps1
pause
# C:\PathToExecutableFile\myjob.ps1
$global:pathname = $MyInvocation.MyCommand.Path
# C:\PathToExecutableFile
$global:path = Split-Path $global:pathname
# myjob.ps1
#$global:file = Split-Path $global:pathname -Leaf
# myjob
$global:name = [System.IO.Path]::GetFileNameWithoutExtension($global:pathname)
# C:\PathToExecutableFile\myjob.exe
$global:pathexe = $global:path + "\" + $global:name + ".exe"
write-host $global:pathexe
$global:user = "mycompany\superman"
$global:pass = "Passw0rd"
#Schedule an exe file to start daily @ 05
#schtasks /create /tn $global:name /ru $global:user /rp $global:pass /rl highest /sc daily /st 05:00 /tr "$global:pathexe"
#Schedule a ps1 file
#schtasks /create /tn $global:name /ru $global:user /rp $global:pass /rl highest /sc daily /st 05:00 /tr "PowerShell.exe -ExecutionPolicy Bypass -NonInteractive -FILE $global:pathps1"
#run working days from 7:45 each 30 min for 10h.
schtasks /create /tn $global:name /ru $global:user /rp $global:pass /rl highest /sc weekly /st 07:45 /sd "18/08/2013" /ri 30 /du:10:00 /d "MON TUE WED THU FRI" /tr "$global:pathexe"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment