Error Creating Scheduled Task

0

I am trying to create a scheduled task using PowerShell 5. But I'm getting an error while trying especially with adding DeleteExpiredTaskAfter to ScheduledTaskSettingsSet

-DeleteExpiredTaskAfter (New-TimeSpan -Seconds 120)

The PowerShell script below:

$taskname = "Test01"
$taskdescription = "Testing"
$taskpath = "\Test"

$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-NoProfile -WindowStyle Hidden -command "& D:\Test.ps1 -SqlServer Sql1 -Database UAT -Date 2018-03-01 -Start 07:00:00"'

$trigger =  New-ScheduledTaskTrigger -Once -At (Get-Date -Month 06 -Year 2018 -Day 21 -Hour 9 -Minute 40 -Second 00)

$setting = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Day 3) -Hidden -StartWhenAvailable -DeleteExpiredTaskAfter (New-TimeSpan -Seconds 120) -RunOnlyIfNetworkAvailable

$principal = New-ScheduledTaskPrincipal -UserId "LOCALSERVICE" -LogonType S4U

function ScheduledTask-Exists($taskname) {
    $schedule = New-Object -Com Schedule.Service
    $schedule.connect()
    $taskFolder = $schedule.GetFolder($taskpath)
    $taskExists = $taskFolder.GetTask($taskname) |
                  Select-Object Name |
                  Where-Object { $_.Name -eq $taskName }
    if (-not $taskExists) {
        return $false
    }
    return $true
}

if ((ScheduledTask-Exists($taskname))) {
    Write-Output "$taskname already exists"  
} else {
    Register-ScheduledTask -Action $action -Trigger $trigger -TaskPath $taskpath -TaskName $taskname -Description $taskdescription -Settings $setting -Principal $principal -Force 
    Write-Output "Creating Scheduled Task - $taskName"
}

Error I'm getting:

Register-ScheduledTask : The task XML is missing a required element or
attribute. (49,4):EndBoundary:
At E:\ARUN\TestScript\AutomateScheduledTask.ps1:33 char:4
+    Register-ScheduledTask -Action $action -Trigger $trigger -TaskPath ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask) [Register-ScheduledTask], CimException
    + FullyQualifiedErrorId : HRESULT 0x80041319,Register-ScheduledTask

I tried adding -Compatibility V1 to New-ScheduledTaskSettingsSet but it didn't work.

powershell
scheduled-tasks
powershell-5.0
taskscheduler
asked on Stack Overflow Jun 21, 2018 by techGaurdian • edited Jun 21, 2018 by Ansgar Wiechers

1 Answer

1
if((ScheduledTask-Exists($taskname))){
   Write-Output "$taskname already exists"  
}
else{
   $run = (Get-Date).AddMinutes(2)
   Register-ScheduledTask -TaskPath $taskpath -TaskName $taskname -InputObject (
  (
    New-ScheduledTask -Action $action -Trigger $trigger -Settings $setting
  ) | %{ $_.Triggers[0].EndBoundary = $run.AddMinutes(30).ToString('s') ; $_} 
)
Write-Output "Creating Scheduled Task - $taskName"
}

Got this script from link stackoverflow answer(2) and modified it, as a work around

answered on Stack Overflow Jun 21, 2018 by techGaurdian • edited Jun 25, 2018 by techGaurdian

User contributions licensed under CC BY-SA 3.0