Conditionally Run a Scheduled Task Interactively or Not
If you have a need to conditionally run a scheduled task interactively with Task Scheduler if a particular user account is logged onto a system or else run it non-interactively then I will outline some steps to get you the expected result based on some conditional logic and with a batch script.
Essentially this solution will require a few simple things. . .
- Define a scheduled task to run interactively with the
Run only when user is logged on
option selected to run if the user account is
logged on with no triggers defined
- Define another scheduled to run non-interactively with the
Run whether user is logged on or not
option selected to run if the user
account is not logged on with no triggers defined
- Create a batch script (example below) that will check if the user account is logged on or not and based on the result execute either the
interactive or non-the interactive scheduled task
- Define another scheduled to run non-interactively with the
Run whether user is logged on or not
option selected to run if the user
account is not logged on with triggers defined for the frequency you
need the jobs to run regardless if interactive or not—so this will be
the scheduled job that executes either of the two other jobs from #1
or #2 above.
Batch Script
This uses query-session and findstr to check whether or not the applicable username is signed onto the machine console. It then uses schtasks to execute either the interactive or non-interactive scheduled tasks that you define without any triggers.
You will want to change the SET username=
value to be what you need it to check for account name wise which you need it to run interactively with that session if it exists (e.g. <Domain>\<Username>
)
@ECHO OFF
SET username=user
query session | findstr /I /M "%username%" | findstr /I /M "console"
IF ERRORLEVEL 0 GOTO :InteractiveTask
GOTO :NonInteractiveTask
:InteractiveTask
SCHTASKS /Run /TN "\_SuperUserTesting\FinalBuilder_i"
EXIT
:NonInteractiveTask
SCHTASKS /Run /TN "\_SuperUserTesting\FinalBuilder_n"
EXIT
Important: You will need to ensure the location and task name full path is specified with the /TN
switch of the schtasks commands to point to the correct task and path location the per hierarchical structure where you saved each with Task Scheduler—see below to screen shots to further clarify.
Furthermore, you will need to ensure the Allow task to be run on demand is set from the Settings tab of each scheduled task which will be executed with schtasks.
Scheduled Tasks
The interactive scheduled task must have the Run only when user is logged on
option checked from the General tab in order for it to be run interactively.
The non-interactively task must have the Run whether user is logged on or not
option checked from the General tab in order for it to be run if that account is not logged onto the machine.
The task that checks to determine which scheduled task to run with the batch script must have the Run whether user is logged on or not
option checked from the General tab in order for it to be run regardless. Other than that, you can setup the Triggers with whatever schedule you desire.
Further Resources
Is savbuildsvc the user that is actively logged on to the computer, meaning that is the active console session? – Appleoddity – 2018-01-13T02:22:54.323
@Appleoddity yes. Basically, this account used to service, maintain, run, etc. This is because this is production server and this account has full access to the resources to which other people have only read access. And there are few other reasons – T.S. – 2018-01-13T16:35:18.117
T.S. I just added another answer to be more accurate per your updated detail disclosing that the account may or may not be logged on and your need to have the scheduled task run in either case. I tested and confirmed this works as expected just as I describe and per your clarification on the topic. – Pimp Juice IT – 2018-01-13T19:13:51.230
1@ITSnuggles Thank you. I will evaluate and test. And will let you know. – T.S. – 2018-01-13T21:16:26.097
@T.S. What's up .... I changed my screen name to something different but it's still my answers below as you can see. Any update on your testing with the newest solution I provided? It's been a few days so I thought I'd ask. – Pimp Juice IT – 2018-01-23T21:42:36.313
1@PimpJuiceIT I was sidelined with something else. Need to get back to it. Will [eventually] let you know, etc – T.S. – 2018-01-24T00:21:25.187
What's up, any update on this by chance? Just curious since it's been 6-7 months or whatever. Wasn't sure if you ever got to the newest post I added back then as it seemed to work rather reliably and exactly as you specified when I tested it. – Pimp Juice IT – 2018-07-25T05:00:00.647
1@PimpJuiceIT Hey, I am very sorry. Bad news first: 1-item lost its priority completely due to less users of build server. 2-I had family issues and was away, working part time. 3-because of #2 I am swamped to the gills and even not active at all on SOF or SU. Good news is that because our team was reduced, our build automation right now is in "pimp mode". + my boss is not going to be around next week so nobody will be asking why I spent time for "things" destined to back burner. So, I probably can give it a shot next week. Thank you for your patience – T.S. – 2018-07-26T05:10:52.930