Powershell v4. Create remote task scheduler task set to expire and delete

后端 未结 3 1746
鱼传尺愫
鱼传尺愫 2020-12-22 02:05

I am trying to create a task that essentially reboots the server but the task is put there by a remote server running a check to see if a reboot is needed. I am stuck trying

3条回答
  •  再見小時候
    2020-12-22 02:34

    This error is caused by a bug introduced in the Task Scheduler back in Vista. Read more here "The task XML is missing a required element or attribute" error when you use the /z switch together with the Schtasks command in Windows Vista

    The work around is to create a task compatible with pre-Windows Vista platforms, if you want to use the -DeleteExpiredTaskAfter parameter.

    This is

    -Compatibility V1
    

    Or in your code

    $settings = New-ScheduledTaskSettingsSet -Compatibility V1 -DeleteExpiredTaskAfter 00:00:01 -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit 01:00:00 -RestartCount 3
    

    There are other things to look out for when setting a scheduled task to expire.

    Valid values.

    There is only a limited number of values that are valid a parameter "-DeleteExpiredTaskAfter". These are Imediately (PT0S), 30 days (P30D), 90 days (P90D), 180 days (P180D) and 365 days (P365D).

    Other Prerequisites

    Before a task can be set to expire there has to be a trigger with an expiration time (EndBoundary) set.

    This is where I'm stuck at the moment, because new PS4.0 cmdlets don't seem to cater for this.

    In the meantime here is an "old school" way of setting up a scheduled task directly via Scheduler Service COM interface.

    $server = "...."
    $domain = $server
    $user = "...."
    $password = "...."
    
    $ExecuteTime = (Get-Date).AddDays(1)
    $ExpireTime = $ExecuteTime.AddMinutes(1)
    
    $taskname = "Reboot $server"
    $taskpath  = "PendingReboots"
    $taskdesc = "Server Reboot"
    
    $ShedService = new-object -comobject "Schedule.Service"
    $ShedService.Connect($server, $user, $domain, $password)
    
    $Task = $ShedService.NewTask(0)
    $Task.RegistrationInfo.Description = "$taskdesc"
    $Task.Settings.Enabled = $true
    $Task.Settings.AllowDemandStart = $true
    $Task.Settings.DeleteExpiredTaskAfter = "PT0S"
    $Task.Settings.ExecutionTimeLimit = "PT1H"
    $Task.Settings.StopIfGoingOnBatteries = $false
    $Task.Settings.RestartCount = 3
    
    $trigger = $task.triggers.Create(1) # Creates a "One time" trigger
    #    TASK_TRIGGER_EVENT     0
    #    TASK_TRIGGER_TIME      1
    #    TASK_TRIGGER_DAILY     2
    #    TASK_TRIGGER_WEEKLY    3
    #    TASK_TRIGGER_MONTHLY   4
    #    TASK_TRIGGER_MONTHLYDOW    5
    #    TASK_TRIGGER_IDLE      6
    #    TASK_TRIGGER_REGISTRATION  7
    #    TASK_TRIGGER_BOOT      8
    #    TASK_TRIGGER_LOGON     9
    #    TASK_TRIGGER_SESSION_STATE_CHANGE  11
    
    $trigger.StartBoundary = $ExecuteTime.ToString("yyyy-MM-dd'T'HH:mm:ss")
    $trigger.EndBoundary = $ExpireTime.ToString("yyyy-MM-dd'T'HH:mm:ss")
    $trigger.Enabled = $true
    
    $Action = $Task.Actions.Create(0)
    $action.Path = "shutdown.exe"
    $action.Arguments = " -r -f -t 0"
    
    Try {$taskFolder = $ShedService.GetFolder("\$taskpath")}
    catch {$taskFolder = $ShedService.GetFolder("\").CreateFolder("$taskpath")}
    
    $result = $taskFolder.RegisterTaskDefinition("$TaskName",$Task,6,"System",$null,5)
    

提交回复
热议问题