I have wrote the following .ps1 file:-
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$request = [System.Net.WebRequest]::Create("")
$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=""> <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> <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 3 Answers
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.exeAdd 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.
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!
2Or simply in "Action" tab fill Program/script: powershellAddarguments (optional): -NonInteractive -file "C:\yourscript.ps1"