Scheduled Task with a Logon Delay

Creating a Scheduled Task to run at logon with a delay is somewhat cumbersome with PowerShell, so as a result, you need to call the Task Scheduler Scripting Objects.

Below walks you through it.

$adminname = 'MyUser'
$adminpassword = 'password'
$taskName = "Start Studio"
$ShedService = New-Object comobject 'Schedule.Service'
$ShedService.Connect()
$Task = $ShedService.NewTask(0)
$Task.RegistrationInfo.Description = $taskName
$Task.Settings.Enabled = $true
$Task.Settings.AllowDemandStart = $true
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa383987(v=vs.85).aspx
$trigger = $task.triggers.Create(9) # 0 EventTrigger, 1 TimeTrigger, 2 DailyTrigger, 3 WeeklyTrigger, 4 MonthlyTrigger, 5 MonthlyDOWTrigger, 6 IdleTrigger, 7 RegistrationTrigger, 8 BootTrigger, 9 LogonTrigger
$trigger.Delay = 'PT1M'
$trigger.Enabled = $true
$action = $Task.Actions.Create(0)
$action.Path = '%ProgramFiles%\StationPlaylist\Studio\SPLStudio.exe'
#$action.Arguments = '-ExecutionPolicy Unrestricted -File c:\UserLogonScript.ps1'
$action.WorkingDirectory = '%ProgramFiles%\StationPlaylist\Studio'
$taskFolder = $ShedService.GetFolder("\")
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa382577(v=vs.85).aspx
$taskFolder.RegisterTaskDefinition($taskName, $Task , 6, $adminname, $adminpassword, 3)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s