Link to home
Start Free TrialLog in
Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)Flag for United States of America

asked on

Create Scheduled Task "Run only when user is logged in" Not Working

Creating a scheduled task using bat script command:

SCHTASKS /Create /RU "user" /RP "password" /TN "name" /XML "file.xml" /F

Nothing in the XML seems to set the option to "Run only when user is logged in". Adding the /it option on the SCHTASKS command doesn't make any difference other than generating this warning:

WARNING: The task is registered, but not all specified triggers will start the task, check task scheduler event log for detailed information.

How can I create a scheduled task and set the "Run only when user is logged in" option using scripts?

Kevin
Avatar of Robert
Robert
Flag of United States of America image

This is set under the logon type the the xml.
it should be 
<LogonType>InteractiveToken</LogonType>

Open in new window


note using this method, should the user change the password, the task will stop running.

what is the environment like, you could use GPO to have the user on logon, create the task.
IS this supposed to run on logon?
Avatar of zorvek (Kevin Jones)

ASKER

I had read the same. But it doesn't seem to make any difference. When I set the setting to "Run whether user is logged in or not", the XML setting remains unchanged.

And, if this were the case, to what would I set it to enable "Run whether user is logged in or not"?

And, if this were the case, why, whenever I create the new scheduled task with that set in the XML, does it always set the task to "Run whether user is logged in or not"?

Kevin
It's supposed to run at a specific time daily.

Kevin
Unattended.

Kevin
Login type is set to "password" when it is set to "Run whether user is logged in or not"

<LogonType>Password</LogonType>

Open in new window

Easiest way is to create a test one using the GUI then right click on it and select export. It will generate an xml file for you.
The XML file is prepared by you, it is not generated by the schatsk .create directive.

you need to include the /IT option when creating the task.

Your task creation statement does not include any scheduling directive.

you need to schedule it
/sd start date /st start time /sc daily /IT

schtasks /create /? provides examples.


what do you have in the XML file?
Can you use Power Shell?

$processinfo = Get-WmiObject -Query "select * from win32_process where name='explorer.exe'" 

    $user = $processinfo | ForEach-Object { $_.GetOwner().User }

    if ($user -ne "User Name") {
         "Not the user"
         exit
    }
    #### Put here what the script supposed to do

Schedule this script at required time as you explained. The script starts, checks the user name, and if it is wrong - exit. If correct - continue processing. 
I have tried the /IT option and the XML has "InteractiveToken" for LoginType and yet it continues to be created with "Run whether user is logged in or not".

When I include /IT in the options passed to SCHTASKS /Create, it generates this warning:

"WARNING: The task is registered, but not all specified triggers will start the task, check task scheduler event log for detailed information."

And the option, again, is "Run whether user is logged in or not" versus "Run only when user is logged on".
 
I'm fine with PowerShell but need a bit of assistance with deleting and creating the tasks.

Kevin
You may have an option set in the XML, that alters what you want.

Your terminology, unattended conflicts in a way with your requirement that the user must be logged in.

Have you tried creating the task as the user versus as an administrator to run as the user?
Have you tried creating the task as the user versus as an administrator to run as the user? 
Yes. ERROR: Access is denied.

You may have an option set in the XML, that alters what you want.

Here is the entire XML:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2021-01-01T00:00:00</Date>
    <Author>xxx</Author>
    <URI>\zorvek\Scheduled Tasks - Daily at 06.05 AM</URI>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2021-01-01T06:05:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-21-160861321-1018265093-554249141-1007</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>true</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>true</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>WScript.exe</Command>
      <Arguments>"xxx"</Arguments>
    </Exec>
  </Actions>
</Task>

And the bat script command to install it:

SCHTASKS /Create /RU "xxx" /RP "xxx" /TN "xxx" /F /IT

Kevin
are you saying that if the user is not logged in, the task runs?

lets try this, what does the task supposed to do?

You have the task to run 6am daily
and you want it to run, only when a specific user is logged in.
From my OP:

"Creating a scheduled task using bat script command:

SCHTASKS /Create /RU "user" /RP "password" /TN "name" /XML "file.xml" /F

Nothing in the XML seems to set the option to "Run only when user is logged in". Adding the /it option on the SCHTASKS command doesn't make any difference other than generating this warning:

WARNING: The task is registered, but not all specified triggers will start the task, check task scheduler event log for detailed information.

How can I create a scheduled task and set the "Run only when user is logged in" option using scripts?"

Kevin
are you saying that if the user is not logged in, the task runs?
That is not the problem.

lets try this, what does the task supposed to do?
The task is supposed to run every day at 6:05 AM. It runs a bat script. It is only supposed to run if the user (me) is logged in. That works just peachy.

You have the task to run 6am daily
6:05 AM to be exact.

and you want it to run, only when a specific user is logged in.
Correct.

Kevin
Why not specify all your parameters on the command line.

schtasks /create /tn /sc daily /st 06:05 /it /ru /tr ...

Let's trying the reverse.

If you are not logged in, does the task run anyway at 6:05?
If you are not logged in, does the task run anyway at 6:05?
The question and answer are not relevant to the problem.

Using a bat script, I am trying to create a task with the option "Run only when user is logged in" set on and am unable to do so - it is always set to "Run whether user is logged in or not". I have ensured that the LogonType in the XML is InteractiveToken and included the /IT option when calling SCHTASKS /Create command.

When I run the command without elevation I get permission errors.

When I add the /IT option I get a warning:

WARNING: The task is registered, but not all specified triggers will start the task, check task scheduler event log for detailed information.

As I have stated above, the task, once installed, runs perfectly. It's the installing part that is not working. I can easily set the task up manually but that's not the problem. The problem is trying to create it using a bat script.

Kevin
Does the batch script run in elevated mode?

Are you in an AD environment?
Does the batch script run in elevated mode?
Yes.

Are you in an AD environment?
Don't know what that is.

Kevin
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Windows environment with a Centrally Managed Active Directory?
OBda might be on track, if the password is not provided, only an existing InteractiveToken available is when the user is logged in.
Windows environment with a Centrally Managed Active Directory?
No