2

I'm having issues with creating scheduled task remotely on a computer.

This is my script:

$Action = New-ScheduledTaskAction -Execute "Robocopy" -Argument"""$downloadDirectory"" ""$fullDirectory"" /e /mt:32 /r:2 /w:2 /z /eta"
Register-ScheduledTask -User (Get-CimInstance –ClassName Win32_ComputerSystem | Select-Object -expand UserName) -Action $Action  -TaskName "ScriptUtilFileCopy" -Force 
Start-ScheduledTask -TaskName "ScriptUtilFileCopy" 

The scheduled task will be run as the local user, not the account used to remote in with.

However, when I run Start-ScheduledTask remotely, the task is set to Queued with the code 0x0 in the Task Scheduler interface. But, if I run the task from Task Scheduled (ie. Right-Click --> Run), the task is run successfully.

Tobias KKS
  • 130
  • 1
  • 4
  • 17
  • What exactly do you mean with `remotely`? Do you connect to a remote system using `Enter-PSSession` or `Invoke-Command` to create the task? – TobyU Dec 17 '18 at 13:52
  • @TobyU Hi, this is done by using `Invoke-Command` – Tobias KKS Dec 17 '18 at 13:54
  • Do you have the same Issue whilste running `Get-ScheduledTask -TaskName "ScriptUtilFileCopy" | Start-ScheduledTask` after registering it? (maybe within a second `Invoke-Command`) – TobyU Dec 17 '18 at 14:05
  • @TobyU Just tried it, still produces the same error. Task Scheduler says `Task Scheduler queued instance of task \ScriptUtilFileCopy.` Last run result also says `0x0` – Tobias KKS Dec 17 '18 at 14:13
  • Have you checked the event log for any errors? – montonero Dec 17 '18 at 14:29
  • I had this issue a while ago with just one server. This article describes the issue I had as well as the solution to fix it (at least in my case): https://www.reddit.com/r/sysadmin/comments/93uk01/fix_scheduled_task_get_queued_and_wont_run_server/ – TobyU Dec 17 '18 at 14:36
  • @TobyU I'm in a Windows 10 environment. This script is being run on client PC's so I'm not sure if that solution is applicable to me. – Tobias KKS Dec 18 '18 at 08:08
  • @montonero Event logs states that a new instance of the scheduled task was created and that it was launched. – Tobias KKS Dec 18 '18 at 08:09
  • You could try to monitor the task with a Process Monitor to see what is really happening when the task is run. – montonero Dec 18 '18 at 08:14

1 Answers1

2

I had exactly the same problem and in my case, it was because of checked condition "Start the task only if the computer is on AC power". And laptop where I test this was really on battery.

After unchecking it, I was able to start it using Start-ScheduledTask.