Question:
My script is like-
1 2 3 4 5 6 7 8 |
$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
1 2 3 4 5 6 7 8 9 10 11 12 |
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
Answer:
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.
1 2 3 |
# Open the Task Scheduler com object $service = new-object -ComObject("Schedule.Service") |