0

I'm a newbie to Windows Server so I'm not sure if I'm doing something wrong, or if the server is at fault here.

I come from a Linux background and I wanted to make something similar to a cronjob. In Windows Server, this seems to be performed by the Task Scheduler. I created a simple .ps1 script to be executed at time XX:YY and also to be executed when I login.

Get-Date | Out-File C:\tmp\debug\debug.txt -Append

After testing it, the history log says "Warning ..... 325 ... queued ... XXXXXX". I've Googled for a solution, but the only thing Google tells me is to let it run parallel in case an existing instance of the process is already running. I've tried all that but it still puts the script in a queue. This queue doesn't seem to be processed.

I would appreciate any help.

Albert
  • 3
  • 6
  • 1
    What is the command line (minus any sensitive information) that you're using to run your script? Is this warning coming from output of a program the script is running, or actually showing up in Task Scheduler? How long would you expect this task to take to run and complete? – SamErde May 25 '21 at 17:11
  • Please update with code you're trying to execute, the complete configuration of the scheduled task, and if any other tasks are scheduled to be executed on login or at the same time. – Colyn1337 May 25 '21 at 21:15
  • @SturdyErde it's just appending Get-Date in a file. Executing this task manually works perfectly fine. Scheduling it doesn't do anything. – Albert May 27 '21 at 09:34
  • @SturdyErde these are simple tasks. For debugging purposes I've had them all deactivated and ended (multiple times) with the exception of a reboot task. Even this task got queued. – Albert May 27 '21 at 10:19
  • Is this task scheduled to run as the same account that you're doing your interactive testing with, or something different? (Permissions?) – SamErde May 27 '21 at 13:33
  • As noted in my answer below, if tasks are still queuing, that would suggest a preceding task has not completed running yet. Something in your script may be holding it up. Is the file literally just that one line? If so, you could try adding an exit code at the end, like "Exit 0" (or fancier, if you actually want to trap a specific exit code). – SamErde May 27 '21 at 13:39
  • Also, this conversation is only one example of why I was also curious about the entire command line that you used in the task action. https://serverfault.com/questions/782458/scheduled-task-runs-with-exit-code-0-but-powershell-script-is-not-always-execut – SamErde May 27 '21 at 13:50
  • @SturdyErde unfortunately, even after disabling and ending all other tasks, a simple reboot task (shutdown.exe /r) gets queued. – Albert May 31 '21 at 07:23
  • Out of curiosity, if you remove the 'at logon' trigger and only use the time-based trigger, does it run at the scheduled time, or does that even queue as well? – SamErde Jun 01 '21 at 14:30
  • @SturdyErde unfortunately yes :( – Albert Jun 02 '21 at 20:09

2 Answers2

2

It sounds like you may already have a few instances of the task queued up, with no timeout configured, and no termination in the script to handle potential errors.

I would recommend starting with a clean slate in the Task Scheduler by ending any existing instances of the task. Right-click on it and chose "End". You may need to do this multiple times if many instances are queued up.

There are a few things that you can tweak in the scheduled task's properties to help keep it moving.

Open the task properties dialog and go to the Settings tab:

  • As you have found you can set a new instance of the task to run in parallel. That may or may not be what you want. Your options there are: "Do not start a new instance", "Run a new instance in parallel", "Queue a new instance", or "Stop the existing instance."
  • Based on how long you expect this task's action to normally take, you probably want to change the setting to "Stop the task if it runs longer than [x] [units of time]. This setting can also be changed within each trigger's settings (see the Triggers tab of the task properties.)

In the task properties dialog, go to the Conditions tab:

  • Unless absolutely required, disable all of the conditions on this tab.

In the task properties dialog, scroll through the History tab to see if there is any status logged other than the "queued" ones. They may provide some insight into what is happening.

All that said, this will just help you run more instances of the task, but it won't solve why the task is not finishing on its own. It can be very helpful in these situations to add step-by-step logging into your PowerShell script so you can gain insight into how far that script is getting before things hang up. My suspicion is that you'll find the script is not completing, which results in the scheduled task never completing, and the next instance of the task never starting. Logging can reveal where this happens, because sometimes running a script manually isn't an exactly perfect test for what happens when the task scheduler runs your script.

As your updated question notes, you're just sending one simple command, so logging may not be very helpful in the long term, but still may reveal what the actual error is. (Try experimenting with the built-in $error variable.) It may be a permissions error if you are running the script as a different account than what you are testing with.

Finally, try adding an "Exit 0" line to the end of your script to see if that helps the Task Scheduler know that the script has ended successfully.

SamErde
  • 3,324
  • 3
  • 23
  • 42
  • Is there a way to actually see what is currently in the queue? I have ended processes multiple times but after each reboot or logon, all tasks are back in the queue. – Albert May 27 '21 at 09:11
  • The new task instances are happening because you added "at logon" as a trigger for the task (in addition to "at time xx:yy"). Just remove that trigger from the task properties, and it won't restart after every logon. – SamErde May 27 '21 at 13:30
  • Sorry for the misconception. I'm trying to debug why all tasks (most of which are triggered by logon) are queued and not executed. My own task is for debugging and gets queued as well, regardless of the type of trigger or if any other items are queued at all. – Albert May 31 '21 at 07:03
  • Right, and did you check the parallel task settings to see if they were set to allow multiple, to queue, or to end the current running task? (Described in my answer above.) – SamErde May 31 '21 at 11:06
  • yes I did. I thought that a parallel running task would skip a queue and be executed as soon as it is triggered. – Albert May 31 '21 at 11:35
  • It should. Are there other tasks still set to not allow simultaneous tasks? This setting should only affect the queue of the individual tasks and not all, bit for troubleshooting purposes it may help to check them all. – SamErde May 31 '21 at 17:32
  • I suspect that is has to do with the environment. When I disable all tasks with the exception of "CreateExplorerShellUnelevatedTask", it will be queued when it is triggered. – Albert Jun 01 '21 at 09:09
0

I wouldnt think a script like that would be queued (its so short would take a mili sec to run). Its more likely something else is at play here. Try to run it manually, it might be that your script is not allowed to run, not signed : https:/go.microsoft.com/fwlink/?LinkID=135170

Notice also which account has permission to execute, especially with schedular if you allow it should be for the accounts you allow.

Peter
  • 115
  • 7
  • When I execute it directly in the task scheduler, it gets executed immediately without being queued. – Albert Jun 01 '21 at 09:04
  • You might check if you have another powershell script active at that moment, or wait for it to finish, the trouble is you often dont see the console so you dont see the error why something is hanging when executing that way. – Peter Jun 01 '21 at 09:19
  • How would I check that? Are there any logs for that? Is there another collection of scripts that are running? Is there an observer of some type that has an overview of this? Can this also hold true if I just execute "shutdown.exe /r", since it's not an explicit script running? – Albert Jun 01 '21 at 09:24
  • 1
    sysinternals has a nice suite of tools proces explorer can show what processes are active. for exmaple pslist, and psexplorer show running processes. – Peter Jun 01 '21 at 09:45