Need to run schedule task as "not logged in"

Topics: Errors
Oct 8, 2014 at 8:58 PM
Edited Oct 9, 2014 at 10:10 PM
I've searched a lot to try to resolve this issue. It seems like I'm doing the correct things (td.Principal.LogonType = TaskLogonType.Password and providing a login/password). The scheduled task is created but when I look at the scheduled task it always has the "Run only if logged in" checkbox checked. If I uncheck that box and save it, it works perfectly. So it seems like I'm close.

Here's the code:
    Sub ScheduleWithTaskManager(Scheduleid As Integer, OneTime As Boolean)
            Dim onetimestring As String = "0"
            If OneTime Then onetimestring = "1"

            Using ts As New TaskService("", "userid", "domain", "pwd")
                ' Create a new task definition and assign properties
                Dim td As TaskDefinition = ts.NewTask
                td.Principal.LogonType = TaskLogonType.Password
                td.RegistrationInfo.Description = Scheduleid & " - " & Session("ClientID") & " - " & GetName(Me.ddlPages.SelectedItem.Text)
                td.Settings.RunOnlyIfLoggedOn = False
                If OneTime Then
                    Dim tt As TimeTrigger = New TimeTrigger
                    tt.StartBoundary = GetNextExecution(OneTime)
                ElseIf ddlPeriodUnits.SelectedValue = "Day" Then
                    Dim dt As DailyTrigger = New DailyTrigger()
                    dt.StartBoundary = GetNextExecution()
                    dt.DaysInterval = 1
                ElseIf ddlPeriodUnits.SelectedValue = "Week" Then
                    Dim wt As WeeklyTrigger = New WeeklyTrigger
                    wt.StartBoundary = GetNextExecution()
                    wt.DaysOfWeek = GetDayoftheweek(ddlDayOf.SelectedValue)
                ElseIf ddlPeriodUnits.SelectedValue = "Month" Then
                    Dim mt As MonthlyTrigger = New MonthlyTrigger
                    mt.StartBoundary = GetNextExecution()
                    If ddlDayOf.SelectedValue = 0 Then
                        mt.RunOnLastDayOfMonth = True
                        Dim dayof(1) As Integer
                        dayof(0) = ddlDayOf.SelectedValue
                        mt.DaysOfMonth = dayof
                    End If
                End If
                    'td.Settings.RunOnlyIfLoggedOn = False
                    Dim domain As String = "" 'Request.ServerVariables("HTTP_HOST")
                    Dim path As String = Server.MapPath("~/bin")
                    Dim arguments As String = onetimestring & " " & Scheduleid & " 40 " & domain
                    td.Actions.Add(New ExecAction(path & "/RunReport.exe", arguments, path))
                    ' Register the task in the root folder
                    ts.RootFolder.RegisterTaskDefinition(Scheduleid, td)
                Catch ex As Exception
                    lbErrMsg.Text = "Problem with registering with scheduler: " & ex.Message
                End Try
            End Using

        End Sub
Thanks for any help on this.
Oct 9, 2014 at 11:18 AM
You must add the username, password and special TaskLogonType. Like this example:
RegisterTaskDefinition(TaskName.Text, td, TaskCreation.CreateOrUpdate,
                                                     usercred.username, TDES.decryptString(usercred.password),
Marked as answer by dahall on 10/22/2014 at 11:00 PM