2

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.

T. Staben
  • 53
  • 1
  • 7
  • `$Settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 30) -DeleteExpiredTaskAfter (New-TimeSpan -Seconds 0)` But I think 0 is just going to be null, so it would like not using that parameter. – BenH Mar 16 '17 at 20:43

1 Answers1

3

Eventually was able to get this working. I found an additional StackOverflow post and an article that helped get me in the right direction:

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

https://blogs.technet.microsoft.com/platformspfe/2015/10/26/configuring-advanced-scheduled-task-parameters-using-powershell/

The working script (at least as far as getting this specific aspect working) is:

function Create-Task ($Computer,$Time)
{

    $ActionArgs = "/c `"Shutdown /m \\$Computer /r /t 0`""
    $TaskAction = New-ScheduledTaskAction -Execute 'cmd.exe' -Argument $ActionArgs 
    $TaskUserName = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount
    $TaskSettings = New-ScheduledTaskSettingsSet -Compatibility V1 -ExecutionTimeLimit (New-TimeSpan -Minutes 30) -DeleteExpiredTaskAfter (New-TimeSpan -Seconds 0)
    $TaskTrigger = New-ScheduledTaskTrigger -Once -At $Time
    $Task = New-ScheduledTask -Action $TaskAction -Trigger $TaskTrigger -Principal $TaskUserName -Settings $TaskSettings

    Register-ScheduledTask -TaskName "Test" -InputObject $Task -Force

    $RegisteredTask = Get-ScheduledTask -TaskName "Test"
    $RegisteredTask.Triggers[0].EndBoundary = $Time.AddMinutes(30).ToString('s')
    $RegisteredTask.Settings.DeleteExpiredTaskAfter = 'PT0S'
    $RegisteredTask | Set-ScheduledTask -User "NT AUTHORITY\SYSTEM"


}

$Comp = "WIN7-0031B"
[datetime]$TriggerTime = '3/17/2017 11:00PM'
Create-Task -Computer $Comp -Time $TriggerTime

One thing I also noticed I was doing wrong from the onset is I was confusing ExecutionTimeLimit with the EndBoundary property.

Also for reference, if you try running this in ISE without running ISE as admin you will most likely get an Access Denied issue. I'm planning to basically have this script as a scheduled task that looks for a CSV file to create the other tasks and will be running it as system so it shouldn't be a problem. But just in case someone happens across this thread and runs into the issue

Community
  • 1
  • 1
T. Staben
  • 53
  • 1
  • 7