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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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) |