Share via


Running PowerShell Script using Task scheduler displaying the 'Last Run Result' as 0xFFFD0000 or The task is currently running. 0x41301

Question

Friday, March 2, 2018 9:41 PM

It's PowerShell script which is running fine from command prompt or PS window but Task scheduler always gives the error 0xFFFD0000.

Changing 'Action' setting to gives 'Last Run Result' as The task is currently running. 0x41301

c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe
–Noninteractive -Noexit -NoProfile -Executionpolicy bypass -File "C:\xxxx\PowerShell-CTSMD\CTSMD_Task_Status_Email_TEST.ps1

CTSMD Task Status Email.ps1

___________________________________________________________________________________

# This code sets up the HTML table in a more friendly format

$style = @'
<style type="text/css">
h4, h5, th { text-align: center; }
table { margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; }
th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; }
td { font-size: 12px; padding: 5px 20px; color: #000;  text-align: right; font-weight: bold }
tr { background: #b8d1f3; }
tr:nth-child(even) { background: yellow }
tr:nth-child(odd) { background: #b8d1f3; }
/* unvisited link */
a:link {
    color: red;
}

/* visited link */
a:visited {
    color: green;
}

/* mouse over link */
a:hover {
    color: hotpink;
}

/* selected link */
a:active {
    color: blue;
}
a:visited {
    text-decoration: none;
}
</style>
'@

# This code defines the search string in the STO database table
$SQLServer = "XXXX####\XXXXXXX1"
$SQLDBName = "XXXXXDB"
$SQLQuery = "SELECT qu_id AS 'Queue ID', qu_task AS 'Task Name', qu_status AS 'Current Status', dbo.tmbatch.pb_pages 
        AS 'Pages', dbo.tmbatch.pb_SCANOP AS 'User', qu_start AS 'Queue Started', dbo.tmbatch.pb_batch AS 'Batch'
                    FROM [XXXXXEngDB].[dbo].[queue]
                        LEFT JOIN [XXXXXEngDB].[dbo].[tmbatch] ON qu_batch = tmbatch.pb_batch
                            where (qu_status = 'pending' or qu_status = 'hold' or qu_status = 'aborted') and qu_task != 'iVScan'
                                ORDER BY qu_id, qu_start, dbo.tmbatch.pb_batch;"

# This code connects to the SQL server and retrieves the data
$SQLConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security=SSPI;"

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd

$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)

$SqlConnection.Close()

# This code outputs the retrieved data
$DataSet.Tables #| Format-Table -Auto

#Creation of dynamic link
foreach($Row in $DataSet.Tables[0].Rows)
{
    $URL = 'http://xxxx###:0000/navigator/?desktop=xxxxx&dcShortcut=Verify&dcJob=Web%20Job&dcTask='+$($Row[1])+'&dcAction=executeBatchPluginAction&dcQueueID='+$($Row[0])
    $Row[1] = '<a href="'+$URL+'">'+$($Row[1])+'</a>'

}

# This code emails a report regarding the results of the retrieved data
if($DataSet.Tables[0].Rows.Count -gt 0) {
    $smtpServer = "smtp.xxxxx.yy.zzz"
    $smtpFrom = "FileNet Notifications <[email protected]>"
    $smtpCc = "[email protected]"
    $messageSubject = "Summary Report: New XXXXX App - Datacap Navigator Job Status"
    $heading = "<span style='background: #f75d59; color: white; max-width: 400px; padding: 5px 10px; font-weight: bold'>List of Pending and Aborted jobs.</span><br><br>"
    $heading += "<span style='background: yellow; color: blue; max-width: 400px; padding: 5px 10px; font-weight: bold'>*NOTE: Clicking directly on any of the Verify links in this e-mail will take you to the Datacap Navigator page for the XXXXX application. Sign in to verify any one of the job(s) listed.</span><br><br>"
    $body = $heading + ($DataSet.Tables[0] |select * -ExcludeProperty RowError, RowState, HasErrors, Name, Table, ItemArray | ConvertTo-Html -Head $style)
    
    # Replacing entity with chracter to work as html
    $body = $body -replace '<','<'
    $body = $body -replace '>','>'
    $body = $body -replace '"',''''

    Send-MailMessage -BodyAsHtml -to $smtpTo -cc $smtpCc -Bcc $smtpBcc -subject $messageSubject -smtpserver $smtpServer -from $smtpFrom -body $body

}

___________________________________________________________________________________

Task Scheduler Profile (Exported xml)

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2018-03-02T10:10:21.2674408</Date>
    <Author>xxxxx\aaaa!</Author>
    <Description>Send report for all aborted/pending/hold tasks for Datacap jobs</Description>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2018-03-02T10:09:50.0171766</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>xxxx###\bbbbb</UserId>
      <LogonType>Password</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe</Command>
      <Arguments>–Noninteractive -Noexit -NoProfile -Executionpolicy bypass -File "C:\xxxx\PowerShell-XXXXX\XXXXX_Task_Status_Email_TEST.ps1"</Arguments>
      <WorkingDirectory>C:\xxxx\PowerShell-XXXXX</WorkingDirectory>
    </Exec>
  </Actions>
</Task>

All replies (2)

Monday, March 5, 2018 5:21 AM

Hi,

Based on your question, since there are no relative paths in the script, I suppose setting "Start in (optional)" is unnecessary.
Also, does the user running this task has the proper permissions to query SQL Server?
In addition, Please set the arguments to -ExecutionPolicy ByPass -File "C:\xxxx\PowerShell-CTSMD\CTSMD_Task_Status_Email_TEST.ps1" and see if the issue still remains.

If you need further help, please feel free to let us know.

Best Regards,
Albert

Please remember to mark the replies as an answers if they help.
If you have feedback for TechNet Subscriber Support, contact [email protected]


Wednesday, March 7, 2018 8:30 AM

Hi,

Just want to confirm the current situations. Have you tried the method provided before?

If you already tried them or the issue remains after trying them, please don’t hesitate to tell me. I will do more research and try my best to give you helpful suggestions.

Best Regards,
Albert

Please remember to mark the replies as an answers if they help.
If you have feedback for TechNet Subscriber Support, contact [email protected]