Windows Task Scheduler not running task despite trigger - windows

please keep in mind throughout this that I am quite new to windows automation and powerShell so go easy on me please. My goal was to create an automatic USB cloner in order to copy files if the drive has a specific name. I've sorted the copying and the name thing in powerShell and batch. Now remember the goal is to have the script running 24/7, based on a lot of reading up on this it would seem that Windows Task Scheduler is the Ideal way to do this. So I set up a task where the action is running the powerShell and the trigger is "At log on of any user" (I am the only user anyway). This is all well and good however when I log off and back in to test it, It doesn't work. I investigated and it turns out in Task Scheduler, for the usbClone task the status says "Ready" as opposed to "Running" (which is what it should be). Also I have ticked the "Enabled" button on the trigger. For reference my OS is Windows 10. If there is a flaw in my logic (I wouldn't be surprised) please explain, other than that it should be a simple "run this script on log on" task yet it isn't working. I should also point out that if I manually right click the task and click "Run" it runs just fine. Any help would be much appreciated
EDIT: Please read comments to see what steps I have already taken based on suggestion.
Thanks in advance,
Fane

Related

Running VBScript through task scheduler while computer is locked

I have a script that loads an excel file from some link, and then writes some information from the excel file to a text file. I set up a task on Task Scheduler to run the script and then email the text file, and it works fine while logged on. It does not, however, run while the computer is locked.
Unfortunately, I need the task to be run in the early morning before I get to work. Is there any way to make this work?
Unfortunately I don't think this is possible. This link HERE contains someone with a similar situation that was solved by making the computer "lock" via custom vbscript. However this is not the traditional "Lock" of a computer and actually just disables and removes a bunch of things... Further down they mention it being possible but it's very limited on what can happen... and making windows active is one of the limitations.

How can I find out, from within a .bat- (or .ps1-) script, if it was started by the task scheduler?

I'm actually interested as the question is stated, but what I'm after is a mechanism for temporarily disable the scheduled run (on multiple servers, whith shared disk), while still being able to run it interactively (if the script can be made to tell if it's started from task-scheduler or not). An external flag (the existence of a file) seemed like a simple approach to do that.
First option was using "%SESSIONNAME%" which seems to be empty when started from the scheduler, although that's also the case when started from powershell (with Start-Process, which might be an option for allowed usage while disabled).
EDIT: A second option, using the whoami cmd-command seems more promising, it reports the user as belonging to the group NT AUTHORITY\BATCH when run from task scheduler (but not when run interactively). Could I shoot myself in the foot using this - can a user be added to that group "permanently" behind my back?
We're on Windows Server 2012 R2. PowerShell solution would be interesting, as a wrapper could probably be used (or even modification/rewrite from .bat to .ps1).
Per #Alex K.: passing a separate argument to signal that we're running in a scheduled context works fine. Simple, reliable techniques that don't require you to know anything about third-party components -- in my neck of the woods we call those "wins" rather than "cheats".
If you really want, you can check for what you're literally asking -- if we're running from Task Scheduler -- by checking the properties of our parent process. From PowerShell this is quite doable, don't know about plain old batch files:
$parentpid = (gwmi "win32_process where (ProcessId = $pid)").parentprocessid
$parentname = (gwmi "win32_process where (ProcessId = $parentpid)").name
if ($parentname -eq 'taskeng.exe') {
"I think this is Task Scheduler, or maybe something else a hacker named taskeng.exe, who knows?"
} else {
"This is not Task Scheduler. Probably."
}
Of course, this might break if one day MS renames the process, or indeed if you want to use something else than TS to run the script automatically, or even if the script invokes another process which in turn invokes a script.
Personally, I would definitely go with the explicit flag. If you forget to apply it, it will be obvious, it can't suddenly break one day, and best of all, you can test if your script works correctly without actually having to schedule it first.

Task Scheduler WorkItem Not Running

I have a very specific problem that I need fixing. The major issue is that I don't exactly know how to properly search the remnants of my problem on google. Therefore I am coming to StackOverflow for advice in hopes that someone will assist me.
Summary
So I am writing an application which is to be PCI-compliant for the company which I am starting. The application involves IPC (Inter-process communication) and two "watch-dog" apps which monitor the status of the main application. One of these "watch-dog" applications is an updater (sends an HTTP Request to the server looking for updates of the application).
So! This method which I am using to check the server if the application is up-to-date is using the WinInet library and InternetOpen() to send the request. Then read the response of the page and use GetCookie() to store the response in a buffer. It shall then parse the response accordingly.
If the response says that the current version of my application is less than the version located on the server. It will then tell the customer (user) that their application is out of date, and ask them whether or not they want to update the software. If they choose to update the software, it will perform a Download And Execute from the internet and launch the update-installer on the user's PC.
The Problem
Unfortunately, windows is very "secure" when it comes to having an "unauthorized" application send an HTTP Request to an outgoing url, let alone download something from the server then execute it on the users PC.
My conclusion to this issue was to add the watch-dog programs to the Task Scheduler. That way after the main application is run, it will spawn the watch-dog programs and check if they are running with NTAUTHORITY privileges (which are granted by the Task Scheduler).
After implementing the code to create the task and point it to the location of the watch-dog programs, naming it, writing a description and everything. I executed the program. It ran without errors though here the REAL PROBLEM:
1) Programs executes
2) Spawns watch-dog programs
3) Watchdog program checks for new version
4) Version is found
5) Installer is downloaded, execute ... but
6) The installer does not appear on the screen!
When I run my 'Process Hacker' application to monitor all process actions. I can see that the new installer is download & executed. It is running as NTAUTHORITY/SYSTEM just like the watch-dog programs but it doesn't appear on the system.
I made sure that in my code the status of the window is set to SW_SHOWNORMAL not SW_HIDE. I also made sure that all flags are set accordingly. Though it doesn't appear on the screen!
When I run the application without adding it to the Task Scheduler as my user without NTAUTHORITY/SYSTEM status and just regular user status. It executes (obviously since I am already running as administrator) -- everything works fine. But after adding it to Task Scheduler and having it run with SYSTEM level privileges. The window doesn't appear on the screen visually. Why's that?
I would greatly appreciate anyone that is able to assist me with this problem. Thank-you!
edit 1
Can anyone help me understand how this user applied his fix in the registry? By reading the problem I can somewhat interpret that he had the same issue as me.
App is invisible if started from Task Scheduler without any user logged in
In any case, I am trying to use the advice that Gisley gave me to run the application in Interactive Mode. Possibly going to try to give that a try. Still looking for more answers but I am going to be working no this none-the-less in the meantime.
edit 2
I tried setting the INTERACTIVE FLAG and it had no effect unfortunately.
Allow me to just emphasize my problem:
For example I write a program which has message boxes and put it in a loop.
for
message box
get current pid
make process in the task scheduler
spawn new process as the task scheduler proc with NTAUTHORITY/SYSTEM
kill last proc pid
end for
Then when I execute it:
I get the message box. Then after new process opens with NTAUTHORITY/SYSTEM the message box does not appear anymore.
Same for if I open a calculator for example.
System("cmd.exe start /c calc.exe")
Program runs... opens calculator
Program gets NTAUTHORITY/SYSTEM status
ON the next loop it executes the calc.exe
I see it in my task manager but it doesnt appear on the screen
I hope the above helped emphasize the core of my issue. I dont see the processes opened by the task scheduler process id with NTAUTHORITY/SYSTEM rights... I dont see the procs executed by it on my screen, though I see them in the task manager | process hacker and they are running with NTAUTHORITY/SYSTEM privileges too.
A shot in the dark - try running the task in interactive mode, but you'll need to have a user logged on.
https://superuser.com/questions/616206/run-interactive-task-even-if-user-is-not-logged-on-windows
Alternatively, or additionally, pass parameters to the installer so that it installs silently.
Silent installation of a MSI package

Problems with Windows Task Scheduler

I have two problems with Windows Task Manager:
One, I have a Python script that sends an email notification, through gmail, at the end of the run. This works fine when I run the script itself, but when I run the script through Windows Task Scheduler, the script runs fine, but does not send an email. Why would this happen?
Two, when running the same script through task scheduler on a remote desktop, when I am logged in, but my session is closed, firefox will not open.
Any help would be appreciated, thank you.
The Task manager doesn't display any GUI for the current user.
You need to be login as system.
A similar thing happened with me ( while using PHP ) and I found out the reason that I had put a relative path to a file to be included in the code.
include("./lib/libfile.php");
Later when I replaced it with the absolute path it worked all right.
include("D://code/lib/libfile.php");
You might have a similar problem.
It could be that you need to specify where it starts in. This is important if you make local references in your program. This would also result in your program running fine, but not doing everything it is built to do.
Inside task scheduler, see "Actions", then edit an action. It's the parameter it says is optional.

How to troubleshoot Windows Scheduled Task not running?

In the process of deploying our .net app, I've got about 20 scheduled tasks set up on a server, all of which basically do the same thing: invoke a small .net console app that pulls data from a SQL db and posts it to a web service. Each task invokes a separate copy of the app, each copy having a different lookup ID value in its config file.
All but two of these tasks run reliably every night. Two of the tasks seem to sporadically stop running from time to time, and it's currently a mystery as to why. When they stop running, the scheduled task interface correctly shows their last run date, which is a day or more behind the other tasks, which have continued to run at the scheduled time. The tasks which stopped running do not run again on their own, despite being indicated as scheduled to run every night. There are no errors recorded in the event log or in the scheduled task interface itself. And here's the strangest part to us: If I manually kick off the scheduled task, it runs fine, it invokes the .net console app and everything finishes without anomaly. And then it continues to run fine at its scheduled time, for days or weeks at a time, only to eventually fail, seemingly out of the blue. It appears both tasks always start to fail on the same night.
There's a "Last Result" column that should give you a code related to the task itself running (it's not going to have any kind of exception data). 0 means the task completed without errors. Anything else you can look up and see why the task won't start. If the task still seems to not be running, but you still see a 0 for the Last Result, that means there's something broken in your code, but it's exiting gracefully.
Did you set "Start in" property?
If these .NET console apps need app.config or some files located into their path, you have to set "Start in" property to "c:\your\app\path\, otherwise they start as if they are in the system directory, and they cannot find files they need!
Taskscheduler assumes on 64 bits systems that the applicaiton is 64 bit.
If it is 32 bit launch it from the 32 bit command line, i.e. if you want to run c:\program files (x86)\Myprogram\Program.exe, tell taskscheduler to launch:
%systemroot%\Syswow64\cmd.exe /C "c:\program files (x86)\Myprogram\Program.exe"
This forces it to launch from the 32 bit command-prompt and hence with 32 bit emulation.
I found this super helpful link :https://windowsreport.com/windows-scheduled-tasks-not-running/ for thorough debugging steps for many use cases.
In my case user account with which scheduler was configured to run was locked that stopped execution of scheduled tasks without any logs or reporting problem.
One reason for Scheduled Tasks not running occurs when associating them with a password-less Windows user account: by default Scheduled Tasks are prevented from running with a blank password. If you want to run a Scheduled Task from an account with no password you have to disable a system variable:
Go to: Start > Administrative Tools > Local Security Policy > Security Settings > Local Policies > Security Options
Select: "Accounts: Limit local account use of blank passwords to console logon only"
Disable this variable
Disclaimer: It´s not recommended to have accounts with no password.
The answer to the below SO question may also be highly relevant to people reading this question (but, NB, it describes only one possible specific problem with Scheduled Tasks and I believe neither of these questions is a duplicate of the other):
Why is my Scheduled Task updating its 'Last Run Time' correctly, and giving a 'Last Run Result' of '(0x0)', but still not actually working?
The summary of the answer given to that other question is that Windows 2012 Scheduled Tasks do not see the correct environment variables, including PATH, for the account which the task is set to run as.
In terms of more general Scheduled Task troubleshooting (as asked about in this question), you can test for this particular issue (e.g. running SET > test.txt in the task, as suggested in that answer), and once you can see it happening, you can work around it if it is affecting you.
In my case, the scheduled task wouldn't run even though it said last run was successful (0). It turned out to be that the windows user account that was running the jobs had become locked out. I only realized this because I tried editing the existing scheduled task, set the user account to the same one, then hit OK and it gave me an error about the account being locked out.
Maybe they hung and were still running?
You can click on the extras-menu and choose the menu entry to view the log, then notepad will open a log file from the task planner
I found this page helpful when I was trying to trouble-shoot a misbehaving Scheduled Task:
http://support.microsoft.com/kb/308558
Select View->Details to show the additional information, like Last Run Time, and Status, and this page gave me the meaning of the status/error code:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx
I discovered a similar issue February 23 2023 but my logs reveal it started mid January 2023.
My tasks had been working for many months without issue. The I began seeing the same error: The system cannot find the file specified. After all day testing and searching I found that a Windows update on Jan 10 2023 changed the way white space is handled. Formerly only needed one set of quotes. The command line help explains:
C:\> schtasks /create /?
==> Spaces in file paths can be used by using two sets of quotes, one
set for CMD.EXE and one for SchTasks.exe. The outer quotes for CMD
need to be double quotes; the inner quotes can be single quotes or
escaped double quotes:
SCHTASKS /Create
/tr "'c:\program files\internet explorer\iexplorer.exe' \"c:\log data\today.xml\""

Resources