How to run a scheduled task in task scheduler through powers shell command - powershell

Any Powershell cmdlet to run scheduled task in task scheduler?
We have commands to Disable,Enable,Start,Stop the tasks in task schduler through powershell commands like below.But I didnt find any command to run a scheduled task which is in "Ready" state.Any idea?
Get-ScheduledTask -Taskpath "\TasksFolder\"|enable-ScheduledTask
Get-ScheduledTask -Taskpath "\TasksFolder\"|disble-ScheduledTask
Get-ScheduledTask -Taskpath "\TasksFolder\"|start-ScheduledTask
Get-ScheduledTask -Taskpath "\TasksFolder\"|stop-ScheduledTask

Use the following cmdlet to start a scheduled task from powershell:
Start-ScheduledTask -TaskPath "your task path" -TaskName "your task name"
To stop:
Stop-ScheduledTask -TaskPath "your task path" -TaskName "your task name"

Related

powershell scheduled job fails to run

I am trying to create a scheduled job using windows powershell, but it can not run correctly.
I check the taskschd.msc and run the job through GUI. And the result is like this "System can not find specified file"(My system is in Chinese language and I translate the result message).
Here is the command I use:
$T = New-JobTrigger -Once -At (Get-Date).AddMinutes(5)
Register-ScheduledJob -Name Test -Trigger $T -ScriptBlock {shutdown.exe /h}
Another question is I have imported module PSScheduledJob, but I cannot track scheduled job information by using command Get-Job.
scheduled tasks and scheduled jobs are different things. if I understand you right you want to create a scheduled task (which can also be managed using taskschd.msc). this can be done as follows:
#create a task action
$taskAction = New-ScheduledTaskAction -Execute 'shutdown.exe' -Argument '/h'
#create a task trigger
$taskTrigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(5)
#register scheduled task with actionn and trigger
Register-ScheduledTask -TaskName 'hibernate computer' -Action $taskAction -Trigger $taskTrigger
after registering the scheduled task you can also see/edit in using taskschd.msc

Can you delete a windows task with another task trough powershell?

When using following powershell script which creates a scheduled task to delete another scheduled task, I am confronted with an error (access denied: 0x80070005). When I manually define the task and execute it, it works fine.
# Prep task 2 for taskscheduler > delete task 1 after reboot if not deleted before
$Action2 = New-ScheduledTaskAction -Execute "C:\Windows\system32\cmd.exe" -Argument "/c schtasks /delete /tn 'Computer Forced Reboot'/f"
$Trigger2 = New-ScheduledTaskTrigger -AtLogon
$Settings2 = New-ScheduledTaskSettingsSet -DontStopIfGoingOnBatteries -Compatibility Win8 -RestartCount:10 -RestartInterval (New-TimeSpan -Minutes 1)
$Principal2 = New-ScheduledTaskPrincipal -UserID "domain\$env:UserName" -LogonType Interactive
# Register task 2 to Taskscheduler
Register-ScheduledTask -TaskName "test test" -Action $Action2 -Trigger $Trigger2 -Settings $Settings2 -Principal $Principal2here
What am I doing wrong? Is this not possible (specific rights) trough powershell? Is there a workaround?

PowerShell Start-ScheduledTasks with Wildcard Folder

For Start-Scheduled tasks if I have multiple folders like Test1, Test2, Test3, ... if I have the same ScheduledTask name ie. Run, how can I make Powershell start-scheduledTask run each of those scheduled tasks?
I tried
Start-ScheduledTask -TaskName '\Microsoft\*\Run'
but that does not work.
Get-ScheduledTask -TaskName "Run" |Start-ScheduledTask

Scheduled task created in Powershell doesn't appear in Task Scheduler

I'm creating a schedule task in Powershell, like this:
$action = New-ScheduledTaskAction -Execute cmd.exe -Argument "-c echo %date% %time% >d:\test.txt"
$trigger = New-ScheduledTaskTrigger -Once -At 12am
#$principal = New-ScheduledTaskPrincipal -UserId $env:userdomain\$env:username
$task = New-ScheduledTask -Action $action -Trigger $trigger
$task | Register-ScheduledTask -TaskName MyTask
The task gets registered: It runs (verified via its output to d:\test.txt), and I can also see it in a separate Powershell window:
PS D:\temp> Get-ScheduledTask MyTask | fl
Actions : {MSFT_TaskExecAction}
Author :
Date :
Description :
Documentation :
Principal : MSFT_TaskPrincipal2
SecurityDescriptor :
Settings : MSFT_TaskSettings3
Source :
State : Ready
TaskName : MyTask
TaskPath : \
Triggers : {MSFT_TaskTimeTrigger}
URI : \MyTask
Version :
PSComputerName :
However, when I open Task Scheduler, I don't see my task in there anywhere - it should be in the Task Scheduler Library folder. I've created a task manually in Task Scheduler, and it looks the same in Get-ScheduledTask.
Why isn't my task seen in Task Scheduler?
How can I create a Task in Powershell so it is seen?
Edit:: I'm on Windows 10 64-bit, Anniversary Update.
Run Task Scheduler as Administrator and you'll see more tasks.
Your code is working properly.
Did you tried to refresh your task scheduler using F5 ?
If you have opened the task scheduler before executing, the task doesn't appears. You need to refresh using F5 or right-click (on the folder) -> refresh.
I had a similar issue and my task was under Task Scheduler Library -> Microsoft -> Powershell -> Scheduled Jobs.
Since you don't appear to be using powershell I'd check all of the folders under Task Scheduler Library to see where it is.
I'd also right click Task Scheduler Library and check View -> Show Hidden Scheduled Tasks

Unable to resume a workflow via task scheduler

In a powershell window I run the following workflow:
workflow foo { Suspend-Workflow; "hello world" | Out-File c:\users\weijgerss\desktop\foo.txt }
Then to resume the workflow, I have the following scheduled via task scheduler triggered to run at startup:
Import-Module PSWorkflow
$jobs = Get-Job -state Suspended
$jobs > c:\users\weijgerss\desktop\zqqfff.txt
$resumedJobs = $jobs | resume-job -wait
$resumedJobs | wait-job
# Task scheduler action: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Normal -NoLogo -NoProfile -Command "&'c:\users\weijgerss\desktop\resume.ps1'"
The workflow does not get resumed neither at startup, nor if I manually trigger it via Task Scheduler. The contents of zqqfff.txt indicates that the task scheduler activated powershell cannot see the workflow. A regular powershell window can see the workflow when I run Get-Job.
(Both the normal powershell window and the task scheduler powershell instance run as same user.)
I used procmon to see what's going on and I can see from this that when powershell normally vs taskscheduler it's looking at different workflow persistence paths, namely:
C:\Users\weijgerss\AppData\Local\microsoft\windows\PowerShell\WF\PS\default\S-1-5-21-3519956147-933941082-741972881-500_EL (a normal powershell window uses this folder)
C:\Users\weijgerss\AppData\Local\microsoft\windows\PowerShell\WF\PS\default\S-1-5-21-3519956147-933941082-741972881-500_EL_NI (a task scheduler activated powershell instance uses this folder)
I'm totally stumped. How can I get a task scheduler activated powershell instance to see the same workflows as normal powershell window can?
The below scripts give you a solution that automatically resumes powershell workflows after a reboot/crash using task scheduler at system start up:
resume-workflows.ps1: (the first line below fixes the _NI issue mentioned in the question)
[System.Management.Automation.Remoting.PSSessionConfigurationData]::IsServerManager = $true
Import-Module PSWorkflow
Get-Job -State Suspended | Resume-Job -Wait| Wait-Job
resume-workflows.cmd: (works around a windows 8/server 2012 task scheduler bug)
#rem This is a workaround for task scheduler bug
#rem See: http://support.microsoft.com/kb/2968540
set "USERPROFILE=%USERPROFILE%\..\%USERNAME%"
set "APPDATA=%USERPROFILE%\AppData\Roaming"
set "LOCALAPPDATA=%USERPROFILE%\AppData\Local"
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NonInteractive -WindowStyle Normal -NoLogo -NoProfile -Command "&'c:\path\to\resume-workflows.ps1'"
To put it all together use the following powershell script to shedule resume-workflows.cmd to run at system start up:
$trigger = New-ScheduledTaskTrigger -AtStartup
$action = New-ScheduledTaskAction -Execute "c:\path\to\resume-workflows.cmd"
$currentuser = ([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)
Register-ScheduledTask -TaskName "Resume $($currentuser.Replace('\', '-'))'s Powershell Workflows" `
-Trigger $trigger -Action $action -RunLevel Highest -User $currentuser `
-Password (Read-Host "Enter password for $currentuser")
Enjoy!
(ILSpy, sysinternal's procmon, plenty of google and a dash of windbg were all instrumental in bringing the above answer to you)