Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add multiple Action items for Scheduled Tasks

I have created a scheduled task to execute a PowerShell script. The Script extracts disk information from remote servers. The script and other components work fine. The issue I have is that I am not able to figure out a way to include another Action item for this scheduled task (this part of the script extracts some information from a different set of servers, so like to have them in the same scheduled task).

How I try to do it:

$parlist = @{
  Name = "CloudOnline-DiskReport"
  Trigger = New-JobTrigger -Daily -At "9:15 AM"
  FilePath = "C:\DiskInfo-1stSet.ps1"
  ScheduledJobOption = New-ScheduledJobOption -RunElevated -RequireNetwork -WakeToRun
}

Register-ScheduledJob @parlist

Get-ScheduledJob -Name CloudOnline-DiskReport |
  New-ScheduledTaskAction -Execute "C:\DiskInfo-2ndSet.ps1"

The scheduled task is created successfully and DiskInfo-1stSet.ps1 works fine but another Action task does not get added. I tried a number of variations of New-ScheduledTaskAction, but all to no use.

like image 236
Arsh Avatar asked Jan 13 '15 09:01

Arsh


1 Answers

New-ScheduledTaskAction just creates a new action. You still need to assign the action to a task. Try this:

Get-ScheduledTask -TaskName 'CloudOnline-DiskReport' | ForEach-Object {
  $actions = $_.Actions
  $actions += New-ScheduledTaskAction -Execute 'C:\DiskInfo-2ndSet.ps1'
  Set-ScheduledTask -TaskName $_.TaskName -Action $actions
}

If you want to add all actions at once during task creation you could do it like this (more or less taken from the Examples section of the New-ScheduledTask documentation):

$actions   = (New-ScheduledTaskAction –Execute 'foo.ps1'),
             (New-ScheduledTaskAction –Execute 'bar.ps1')
$trigger   = New-ScheduledTaskTrigger -Daily -At '9:15 AM'
$principal = New-ScheduledTaskPrincipal -UserId 'DOMAIN\user' -RunLevel Highest
$settings  = New-ScheduledTaskSettingsSet -RunOnlyIfNetworkAvailable -WakeToRun
$task      = New-ScheduledTask -Action $actions -Principal $principal -Trigger $trigger -Settings $settings

Register-ScheduledTask 'baz' -InputObject $task
like image 150
Ansgar Wiechers Avatar answered Sep 18 '22 02:09

Ansgar Wiechers