A task inside my windows task scheduler will cause my server to stop responding

0

I have the following AppPoolActivation.ps1 file, to call a URL :-

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

and I wrote the following app.bat file to call the above AppPoolActivation.ps1 file:-

"%SYSTEMROOT%\system32\windowspowershell\v1.0\powershell.exe" -Command Start-Process "$PSHOME\powershell.exe" -Verb RunAs -ArgumentList "'-NoExit %~dp0\AppPoolActivation.ps1'

Then inside my windows server 2012 windows tasks scheduler , I define a Daily task which run each 15 minutes for a duration of 1 day, so mainly it will always run each 15 minutes, and I define this task to call the .bat file, as follow:-

enter image description here

enter image description here

now when I first define the task it worked well for around 5 hours, but I have noted that after 10-12 hours the server will be very slow till it stop responding, now I check the task manager on the server and I find that there will be many instances of windows power shell & Console windows host running, as follow:-

enter image description here

so can anyone advice how I can overcome this problem ? I mean why there will be many instances of windows power shell & Console windows host running ? and is there a way to prevent this from happening and prevent my server to became very slow due to the task schedule ? baring in mind that I have never face any problem on this server for around many years ago ,, the problem started when I create this schedule task...

John John

Posted 2016-02-13T04:08:28.270

Reputation: 235

Answers

1

"%SYSTEMROOT%\system32\windowspowershell\v1.0\powershell.exe" -Command Start-Process "$PSHOME\powershell.exe" -Verb RunAs -ArgumentList "'-NoExit %~dp0\AppPoolActivation.ps1'

So you have a batch file, which starts powershell instance, which starts another powershell instance as Administrator, which executes AppPoolActivation.ps1 script?

Why wouldn't you just put PowerShell.exe in Program/Script field in Task Properties and use -ExecutionPolicy Bypass -File C:\T\AppPoolActivation.ps1 as argument? I doubt that you need to run PowerShell as admin to make web request. And if this issue would still persist, this will make debugging it way easier.

beatcracker

Posted 2016-02-13T04:08:28.270

Reputation: 2 334

I tried your approach at the beginning to call the .ps1 directly from the task, but it never succeed I think the problem is related to a policy that we can not run PowerShell scripts ,,,now what I did I modify the current .bat file and I remove the "-NoExit" from it, and now I can not see repeated processes inside my task manager,, seems -NoExit will not end the process once the powershell script ends, I thought -NoExit will prevent the process from being ended during the execution, but seems it mean to never end the process even after it is being completed.. – John John – 2016-02-15T15:42:33.953

1

@JohnJohn You can bypass execution policy with from Task Manager too. See updated answer. "seems -NoExit will not end the process once the powershell script ends" - yep, see PowerShell.exe Command-Line Help.

– beatcracker – 2016-02-15T17:27:04.947