Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New-ScheduledTaskAction : The term 'New-ScheduledTaskAction' is not recognized as the name of a cmdlet

Tags:

powershell

My script is like-

$action = New-ScheduledTaskAction -Execute "BolusDemoDataGenerator.BAT"
$TaskStartTime = [datetime]::Now.AddMinutes(2) 
$trigger = New-ScheduledTaskTrigger -At $TaskStartTime -Once

$setting = New-ScheduledTaskSettingSet
$inputTask = -action $action  -trigger $trigger -settings
Register-ScheduledTask BatchRunTask -InputObject $inputTask

but I am getting an error

New-ScheduledTaskAction : The term 'New-ScheduledTaskAction' is not 
recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a 
path was included, verify that the path is
correct and try again.
At C:\Users\abc\Desktop\hi.ps1:1 char:11
+ $action = New-ScheduledTaskAction -Execute "BolusDemoDataGenerator.BAT"
+           ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (New-ScheduledTaskAction:String) 
[], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Not sure what is wrong I tried to execute this powershell script to create a task in task scheduler to execute a bat file present in desktop

like image 747
Ashu Avatar asked Sep 07 '17 13:09

Ashu


1 Answers

With PowerShell 4.0 a scheduled task can be easily created with the new cmdlets New-ScheduledTaskAction, New-ScheduledTaskTrigger and Register-ScheduledTask, but unfortunately it's not possible in the prévious versions (have a look to PSVersion in $PSVersionTable).

For previous version you can find many examples where people invoke the schtasks.exe command.

If you want to avoid calling external executables and do as much in PowerShell as possible you can use the Task Scheduler’s com object for creating a scheduled task.

# Open the Task Scheduler com object
$service = new-object -ComObject("Schedule.Service")
like image 191
JPBlanc Avatar answered Oct 23 '22 19:10

JPBlanc