I'm currently attempting to create a Powershell script that the overall goal will create a scheduled task on my VM which will reboot remote computers at certain times. The issue I'm currently having is trying to set the task to expire, and delete once it has been completed.
One point of confusion is there seems to be a disparity between the type of variable the cmdlet wants, and how an existing one is shown in the system. So as an example I have a dummy task that: -Runs once at 11:00PM on 3/16/2017 and expires at 11:30PM on 3/16/2017 in the Triggers tab -Has the setting "If the task is not scheduled to run again, delete it after: Immediately" under settings.
If I look at the existing task using Get-ScheduledTask this is what the values come across as: (Get-ScheduledTask -TaskName Test).Settings
If I do a Get-Member these values are strings (Get-ScheduledTask -TaskName Test).Settings | Get-Member
Now part of my issue may entirely be that I'm using the wrong cmdlets to go about this (just started messing around with this yesterday).The basic script I'm using while testing this out is:
function Create-Task ($Computer,$Time)
{
$Task = New-ScheduledTaskAction -Execute 'cmd.exe'`
-Argument "/c `"Shutdown /m \\$Computer /r /t 0`""
$Trigger = New-ScheduledTaskTrigger -Once -At $Time
$Settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit 'PT72H' -DeleteExpiredTaskAfter 'PT0S'
Register-ScheduledTask -Action $Task -Trigger $Trigger -TaskName "Test" -Description "Testing this out" -Settings $Settings
}
$Comp = "WIN7-0031B"
[datetime]$TriggerTime = '3/16/2017 11:00PM'
Create-Task -Computer $Comp -Time $TriggerTime
When I look at Get-Member for New-ScheduledTaskSettingsSet cmdlet it again says those values are strings
However, if I attempt to run the script as is it complains about them not being datetime variables:
"New-ScheduledTaskSettingsSet : Cannot process argument transformation on parameter 'ExecutionTimeLimit'. Cannot convert value "PT72H" to type "System.TimeSpan". Error: "String was not recognized as a valid TimeSpan."" (I would post a screenshot but I just joined StackOverflow so don't have the reputation).
What I've so far attempted has been changing the $Settings variable to things such as:
$Settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 30).Minutes -DeleteExpiredTaskAfter (New-TimeSpan -Seconds 0).Seconds
and
$Settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 30).Minutes.ToString() -DeleteExpiredTaskAfter (New-TimeSpan -Seconds 0).Seconds.ToString()
In both scenarios it does not generate any error messages, but the setting just doesn't get created. I'm wondering if doing something like this requires creating the task without the setting, and then doing something like a
Get-ScheduledTask -TaskName 'Test' | Set-ScheduledTask <Some other code here>
type of method, but wasn't 100% sure about how to do this. I'm somewhat new to powershell and completely new to trying to create scheduled tasks through Powershell, and any help would be greatly appreciated! The version of Powershell is Version 5
Edit Comment: It looks like when I pasted my script to StackOverflow it is showing most of it as being within quotations and not sure why, but in my ISE it shows things properly.