Windows task schduler keep showing 0x41301

5

1

I have wrote the following .ps1 file:-

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$request = [System.Net.WebRequest]::Create("https://localhost/")
$response = $request.GetResponse()
$response.Close()

and I set a task within the windows server 2012 r2 task scheduler , to call te above ps1 file.now I run this task manually, but it keep showing "0x41301" which means it is still running ... although if I directly run the above code inside PowerShell window it will end in less than a second ? so why calling this ps1 using task Schuler will never ends ?

--Edit-- here is the XML exported:-

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2016-02-09T00:35:11.5514762</Date>
    <Author>ad-services\user.service</Author>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <Repetition>
        <Interval>PT5M</Interval>
        <Duration>PT30M</Duration>
        <StopAtDurationEnd>false</StopAtDurationEnd>
      </Repetition>
      <StartBoundary>2016-02-09T19:30:01</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-20</UserId>
      <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>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>C:\Users\user.service\Documents\AppPoolActivation.ps1</Command>
    </Exec>
  </Actions>
</Task>

John John

Posted 2016-02-08T17:36:37.840

Reputation: 235

1You can enable that the task always ends in x minutes, hours, days, etc. Is it really a problem to kill the task this way? I've seen it before that a script ends, but the process remains active for some reason. – LPChip – 2016-02-08T17:42:59.460

1Can you post the exported XML from your Task so we can see exactly how you're setting it up? – Ƭᴇcʜιᴇ007 – 2016-02-08T17:51:26.473

@LPChip I did not get your point what is wrong with my script ? – John John – 2016-02-09T00:31:04.947

@Ƭᴇcʜιᴇ007 I edit my question with the generated XML.. can you please check ? thanks – John John – 2016-02-09T00:43:21.937

@JohnJohn My point is, that your script can be fine and run until completion, but the process still remains active and that keeps the scheduled task from stopping normally. – LPChip – 2016-02-09T09:40:49.310

Answers

9

I believe this shows the problem:

  <Actions Context="Author">
    <Exec>
      <Command>C:\Users\user.service\Documents\AppPoolActivation.ps1</Command>
    </Exec>
  </Actions>

You shouldn't just put a .PS1 script in as the command you want to run, it will cause it to fail, or do weird things. :)

Instead in the Task, change the "Program/script" you want to run to:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add the script via the "Arguments" field of the task, as well as include Execution Policy changes (if required). i.e:

-ExecutionPolicy Bypass -file "C:\Users\user.service\Documents\AppPoolActivation.ps1"

You may also want to change the "Start in" field to match the path that the script exists in, i.e.: C:\Users\user.service\Documents\.

Piece of advice, don't store the script in a user's profile folder, as it can cause access issues. Instead make a folder (outside of the Users folder) to hold your script(s), and ensure the user account used to run the task has appropriate access.

Ƭᴇcʜιᴇ007

Posted 2016-02-08T17:36:37.840

Reputation: 103 763

I follow your steps and now I last run is showing 0x1 ? – John John – 2016-02-09T15:00:20.853

@JohnJohn 0x1 means succesful if I'm not mistaken. – LPChip – 2016-02-11T08:28:16.043

@Techie007 now the only approach which works in my case, is that I create a .bat file which calls the .pst file,, then inside the task scheduler i specify the path to the .bat file inside the "Program/script" ... – John John – 2016-02-12T16:13:58.427

1@LPChip 0x0 is SUCCESS. Any non-zero return value usually means error. – Dan – 2016-12-08T20:29:30.390

0

Or simply in "Action" tab fill Program/script: powershell Addarguments (optional): -NonInteractive -file "C:\yourscript.ps1"

user881879

Posted 2016-02-08T17:36:37.840

Reputation: 1

-1

I had this problem as I had the task set to "run whether user is logged on or not".. When I switched it to "Run only when user is logged on" and tested it, I realized that I had a PAUSE in one of my batch files that I forgot to remove!

I know this is likely not related but hopefully it helps someone out in the future!

Kyle

Posted 2016-02-08T17:36:37.840

Reputation: 11

1VLQ Reviewers: This is a legitimate attempt to answer the question. A pause command would indeed produce the task status code reported by the OP. – I say Reinstate Monica – 2017-11-28T18:18:35.363

It solve my problem too – sam – 2018-10-31T06:30:15.520