title: Powershell v4. Create remote task scheduler task set to expire and delete
body: <p>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 to add an expiration so it deletes itself but can't find where to put that setting or what it is. It has to do with an end boundry or something and this setting -DeleteExpiredTaskAfter but don't know what value to put in. </p>
  
  <pre><code>$dc = "server2reboot"
  $taskname = "Reboot $DC"
  $taskpath  = "PendingReboots"
  $CimSession = New-CimSession -ComputerName $dc -Credential $credentials -Authentication Negotiate
  
  Function Create-AndRegisterRebootTask{
   Param ($taskname, $taskpath)
   $action = New-ScheduledTaskAction -Execute '#shutdown.exe -r -f -t 0"'
   $trigger =  New-ScheduledTaskTrigger -once -At ("$nextsundaydate 3:00") -RandomDelay 03:00:00 
   Register-ScheduledTask -CimSession $cimsession -RunLevel Highest  -Action $action -Trigger $trigger -TaskName $taskname -Description "Server Reboot" -TaskPath $taskpath -Force
   }
  
  
  
  Function Create-NewRebootTaskSettings{
   Param ($taskname, $taskpath)
   $settings = New-ScheduledTaskSettingsSet -DeleteExpiredTaskAfter "PT0S" -compatability "win8" -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit "PT1H" -RestartCount 3
   Set-ScheduledTask -CimSession $cimsession -TaskName $taskname -Settings $settings -TaskPath $taskpath
   }
  
  Create-AndRegisterRebootTask -taskname $taskname -taskpath $taskpath 
  Create-NewRebootTaskSettings -taskname $taskname -taskpath $taskpath 
  
  Set-ScheduledTask : The task XML is missing a required element or attribute.
  (48,4):EndBoundary:
  At line:5 char:2
  +  Set-ScheduledTask -CimSession $cimsession -TaskName $taskname -Settings $settin ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask) [Set-ScheduledTask], CimException
      + FullyQualifiedErrorId : HRESULT 0x80041319,Set-ScheduledTask
  </code></pre>