Work station lock/unlock triggers

Apr 23, 2012 at 9:07 AM

How can we create a OnWorkStationLock or OnWorkstationUnlock task with code using this wrapper?
In Windows station in the trigger drop down , triggers for this are available, but i cant find the corresponding trigger in the wrapper .

 

Help please.....

Coordinator
Apr 23, 2012 at 10:47 PM
Edited Apr 24, 2012 at 8:29 PM

Add a SessionStateChangeTrigger to your task with the appropriate value assigned to the StateChange property.

new SessionStateChangeTrigger(TaskSessionStateChangeType.SessionLock) { UserId = "joe" };
new SessionStateChangeTrigger(TaskSessionStateChangeType.SessionUnlock);

Apr 24, 2012 at 4:45 AM

Dahall,

I tried that last time also, but its creating a task as a one time task, unlike onworkstation task as created when i create it from the UI. Also when i lock the screen, this task is not getting fired.

Here's my code snippet:

using (TaskService ts = new TaskService(pc, userId, domain, pwd, forceV1))       

        {                   
                    TaskDefinition td = ts.NewTask();                 

   td.RegistrationInfo.Description = "On Workstation Lock";                   

td.Principal.LogonType = TaskLogonType.ServiceAccount;


                    td.Settings.Priority = System.Diagnostics.ProcessPriorityClass.Idle;     

              td.Settings.RunOnlyIfLoggedOn = false;            
                   td.Settings.ExecutionTimeLimit = new TimeSpan(45, 0, 0, 0);
                    td.Triggers.Add(new SessionStateChangeTrigger() { StateChange = TaskSessionStateChangeType.SessionLock, UserId = "apt\\koushikch" });                   td.Actions.Add(new ExecAction(targetTask, "", null));                    
                    ts.RootFolder.RegisterTaskDefinition(taskName, td);

  }

Coordinator
Apr 24, 2012 at 9:01 PM

When I use that trigger, it works as long as my action can be run by the SYSTEM account or if I specify an account and password when I register the task. The history will show failures and successes.

Apr 25, 2012 at 6:54 AM

I already have a OnIdle task and that is executing the same action under the System account.
I need the same action to happen when the workstation is locked.

As you can see, as my requirements of actions are same, so i have created the 2 tasks in the same way, but with different trigger conditions.

The OnIdle task is working perfectly fine, but  this is not working....

Apr 25, 2012 at 7:55 AM

Dahall,

When i export the tasks from task scheduler UI in xml format,

I see difference in the cases:

1. Case when its created by the wrapper :

<Triggers>    <TimeTrigger>      <StartBoundary>2012-04-25T13:24:00</StartBoundary>      <Enabled>true</Enabled>    </TimeTrigger>  </Triggers>

2. Case when its manually created from the UI:

<Triggers>    <SessionStateChangeTrigger>      <Enabled>true</Enabled>      <StateChange>SessionLock</StateChange>    </SessionStateChangeTrigger>  </Triggers>

As you can see the problem is with the trigger type. Its not getting created as a StateChange trigger from the wrapper, as expected

Apr 25, 2012 at 10:36 AM

I sorted it out...my mistake... i was having the forceV1 flag on. It should ve for v2