8

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>
5
  • 1
    You 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
    Feb 8, 2016 at 17:42
  • 1
    Can you post the exported XML from your Task so we can see exactly how you're setting it up? Feb 8, 2016 at 17:51
  • @LPChip I did not get your point what is wrong with my script ?
    – John John
    Feb 9, 2016 at 0:31
  • @Ƭᴇcʜιᴇ007 I edit my question with the generated XML.. can you please check ? thanks
    – John John
    Feb 9, 2016 at 0:43
  • @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
    Feb 9, 2016 at 9:40

3 Answers 3

11

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.

4
  • I follow your steps and now I last run is showing 0x1 ?
    – John John
    Feb 9, 2016 at 15:00
  • @JohnJohn 0x1 means succesful if I'm not mistaken.
    – LPChip
    Feb 11, 2016 at 8:28
  • @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
    Feb 12, 2016 at 16:13
  • 2
    @LPChip 0x0 is SUCCESS. Any non-zero return value usually means error.
    – Dan
    Dec 8, 2016 at 20:29
0

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!

2
  • 1
    VLQ Reviewers: This is a legitimate attempt to answer the question. A pause command would indeed produce the task status code reported by the OP. Nov 28, 2017 at 18:18
  • It solve my problem too
    – sam
    Oct 31, 2018 at 6:30
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.