Running the command below creates the task and has the correct settings. The "if the task is not scheduled to run again delete it after 50 seconds is set. The command takes about 10 seconds to run. The problem is that it never actually deletes the task from task scheduler.
$run = (Get-Date).AddMinutes(1) # Two minutes from now
Register-ScheduledTask -TaskName $taskname -InputObject (
(
New-ScheduledTask -Action (
New-ScheduledTaskAction -Execute "powershell.exe" -Argument (
"C:\Send-HipchatNotification.ps1 -room '$room' -message '$message' -color
'purple' -notify"
)
) -Trigger (
New-ScheduledTaskTrigger -Once -At ($run.TimeOfDay.ToString("hh\:mm"))
# As a "TimeOfDay" to get 24Hr format
) -Settings (
New-ScheduledTaskSettingsSet -DeleteExpiredTaskAfter 00:00:50 #
Delete one second after trigger expires
)
) | %{ $_.Triggers[0].EndBoundary = $run.AddMinutes(60).ToString('s') ; $_
} # Run through a pipe to set the end boundary of the trigger
)`