Jump to content
Sign in to follow this  
kosamja

How to run scheduled task on any user logon?

Recommended Posts

kosamja

How to run scheduled task on any user logon? I tried this but it works only for current user:

#RequireAdmin
#NoTrayIcon
#include <SecurityConstants.au3>

$taskname1 = "Testname1"
MsgBox(0,'','test start')
$result = _LogonTaskCreate($taskname1, @ScriptFullPath, "", "", "Description of this Task")
If $result = True Then
   MsgBox(0, "Success", "The task with the name " & $taskname1 & " was created successfully")
Else
   MsgBox(0, "Failure", "The task with the name " & $taskname1 & " was not created successfully")
   Exit
EndIf
$result = _TaskStart($taskname1)
If $result = 1 Then
   MsgBox(0, "Success", "The task with the name " & $taskname1 & " was started successfully")
Else
   MsgBox(0, "Failure", "The task with the name " & $taskname1 & " was not started successfully")
   Exit
EndIf
$result = _TaskDelete($taskname1)
If $result = 1 Then
   MsgBox(0, "Success", "The Task with the name " & $taskname1 & " was deleted successfully")
Else
   MsgBox(0, "Failure", "The Task with the name " & $taskname1 & " was not deleted successfully")
   Exit
EndIf
Exit

Func _TaskStart($sTaskName, $sTaskFolderTree = '\')
   $oScheduleService = ObjCreate('Schedule.Service')
   If not IsObj($oScheduleService) Then Return SetError(1, 0, False)
   $oScheduleService.Connect()
   $oFolder = $oScheduleService.GetFolder($sTaskFolderTree)
   If not IsObj($oFolder) Then Return SetError(2, 0, False)
   $oTask = $oFolder.GetTask($sTaskName)
   If not IsObj($oTask) Then Return SetError(3, 0, False)
   $oTask.Run(1)
   Return True
EndFunc

Func _TaskIsRunning($sTaskName, $sTaskFolderTree = '\')
   $oScheduleService = ObjCreate('Schedule.Service')
   If not IsObj($oScheduleService) Then Return SetError(1, 0, False)
   $oScheduleService.Connect()
   $oFolder = $oScheduleService.GetFolder($sTaskFolderTree)
   If not IsObj($oFolder) Then Return SetError(2, 0, False)
   $oTask = $oFolder.GetTask($sTaskName)
   If not IsObj($oTask) Then Return SetError(3, 0, False)
   $oTaskInstance = $oTask.GetInstances(0)
   If not IsObj($oTaskInstance) Then Return SetError(4, 0, False)
   If $oTaskInstance.Count() <= 0 Then Return SetError(5, 0, False)
   Return True
EndFunc

Func _TaskDelete($sTaskName, $sTaskFolderTree = '\')
   $oScheduleService = ObjCreate('Schedule.Service')
   If not IsObj($oScheduleService) Then Return SetError(1, 0, False)
   $oScheduleService.Connect()
   $oFolder = $oScheduleService.GetFolder($sTaskFolderTree)
   If not IsObj($oFolder) Then Return SetError(2, 0, False)
   $oFolder.DeleteTask($sTaskName, 0)
   Return True
EndFunc

Func _LogonTaskCreate($sTaskName, $sPath, $sWorkingDir = '', $sParameters = '', $sTaskDescription = '')
   $TASK_TRIGGER_LOGON = 9
   $TASK_RUNLEVEL_HIGHEST = 1
   $TASK_CREATE_OR_UPDATE = 0x6
   $TASK_INSTANCES_IGNORE_NEW = 2
   $TASK_LOGON_INTERACTIVE_TOKEN = 3
   $oScheduleService = ObjCreate('Schedule.Service')
   If not IsObj($oScheduleService) Then Return SetError(1, 0, False)
   $oScheduleService.Connect()
   $oFolder = $oScheduleService.GetFolder('\')
   If not IsObj($oFolder) Then Return SetError(2, 0, False)
   $oTaskDefinition = $oScheduleService.NewTask(0)
   If not IsObj($oTaskDefinition) Then Return SetError(3, 0, False)
   $oRegistrationInfo = $oTaskDefinition.RegistrationInfo()
   If not IsObj($oRegistrationInfo) Then Return SetError(4, 0, False)
   $oRegistrationInfo.Description() = $sTaskDescription
   $oRegistrationInfo.Author() = @LogonDomain & '\' & @UserName
   $oTaskSettings = $oTaskDefinition.Settings()
   If not IsObj($oTaskSettings) Then Return SetError(5, 0, False)
   $oTaskSettings.MultipleInstances() = $TASK_INSTANCES_IGNORE_NEW
   $oTaskSettings.DisallowStartIfOnBatteries() = False
   $oTaskSettings.StopIfGoingOnBatteries() = False
   $oTaskSettings.AllowHardTerminate() = True
   $oTaskSettings.StartWhenAvailable() = True
   $oTaskSettings.RunOnlyIfNetworkAvailable() = False
   $oTaskSettings.Enabled() = True
   $oTaskSettings.Hidden() = False
   $oTaskSettings.RunOnlyIfIdle() = False
   $oTaskSettings.WakeToRun() = False
   $oTaskSettings.ExecutionTimeLimit() = ''
   $oTaskSettings.Priority() = 5
   $oPrincipal = $oTaskDefinition.Principal()
   If not IsObj($oPrincipal) Then Return SetError(6, 0, False)
   $oPrincipal.Id() = @UserName
   $oPrincipal.UserId() = @UserName
   $oPrincipal.DisplayName() = @UserName
   $oPrincipal.LogonType() = $TASK_LOGON_INTERACTIVE_TOKEN
   $oPrincipal.RunLevel() = $TASK_RUNLEVEL_HIGHEST
   $oColTriggers = $oTaskDefinition.Triggers()
   If not IsObj($oColTriggers) Then Return SetError(7, 0, False)
   $oTaskTrigger = $oColTriggers.Create($TASK_TRIGGER_LOGON)
   If not IsObj($oTaskTrigger) Then Return SetError(8, 0, False)
   $oTaskTrigger.Id() = $sTaskName
   $oTaskTrigger.Enabled() = True
   $oTaskTrigger.StartBoundary() = ''
   $oTaskTrigger.EndBoundary() = ''
   $oColActions = $oTaskDefinition.Actions()
   If not IsObj($oColActions) Then Return SetError(9, 0, False)
   $oColActions.Context() = @UserName
   $oTaskAction = $oTaskDefinition.Actions.Create(0)
   If not IsObj($oTaskAction) Then Return SetError(10, 0, False)
   $oTaskAction.Path() = '"' & StringReplace($sPath, '"', '') & '"'
   $oTaskAction.WorkingDirectory() = $sWorkingDir
   $oTaskAction.Arguments() = $sParameters
   $oFolder.RegisterTaskDefinition($sTaskName, $oTaskDefinition, $TASK_CREATE_OR_UPDATE, '', '', $TASK_LOGON_INTERACTIVE_TOKEN, '')
   Return True
EndFunc

 

Share this post


Link to post
Share on other sites
Nunos

Could you compile your script to an exe and put it in the All Users Startup folder or maybe call it from the registry?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×