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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With