Cross Post - Task scheduler intermittently running a powershell script

  • OP

    I've been at this for nearly two weeks, and I'm ready to pull my hair out. I work at the Boston Museum of Science. One of the prototype exhibits I'm working on has a projector that doesn't go into standby mode when the computer goes down, and for various reasons I'm stuck with it in this particular space. So I made an IR remote with an Arduino that I can control through the serial port and machined a bracket to firmly hold the IR LED in place. That part works great.

    I wrote a powershell script that opens up the com port, verifies that an IR code was sent, closes the com port, and then exits with either a success or failure. The script works as expected, and runs perfectly in ISE. I've created a scheduled task that runs this 1 hour before we open, and 15 minutes after we close. Task scheduler also shuts the PC down, and then a bios alarm brings it back up in the morning.

    The problem is that the script doesn't always run, and I'm not sure what to do at this point. So far it's worked 90% in the evening (complete with success code), but it's 50% for the morning startup. Task Scheduler will say that the task is running, but it just sits there till it times out, or it completes exactly on time. I have it set to kill the task after a minute and retry 30 times within that hour, but I'm not sure if that really matters. If I click on the task and choose to run it from Task Scheduler, then it's worked 100% of the time for me.

    I've got all the standards checked, as I've read a ton of things on running powershell scripts in task scheduler. It's set to run as the local admin, whether they are logged on or not, and with the highest available privileges. I'm pointing to C:...\powershell.exe directly, and using the following arguments:
    -noprofile -ExecutionPolicy Bypass -file "C:\PowerShell\toggle projector power.ps1"

    I've got it set to restart every minute (the script is 30 seconds max) for 30 times, stopping the previous instance, or kill it at the end of an hour. Nothing in conditions is checked. I haven't waited the full hour in the mornings that it doesn't work before poking it, but it seems like it either runs immediately and works, or it sits there are says that it's running till the time runs out.

    Anytime I do tests, it works. It runs as configured in TS when I manually press the run button. Also every single test I've done where I create single time tasks to bring the projector down and back up has worked. Any time I change all the tasks to go all the way down and back up, then it works. Out of the 10 days I've been on and off working on this, it has successfully shut down the projector and then itself 9 of those days. Of the mornings, I'm at 5 of them coming up.

    I'm at a loss at this point, but at least it seems to be shutting down the projector, which is an improvement.

  • Why are you passing -noprofile when you are running the process from an administrative account?

    Did you open task scheduler as an administrator and create the process there?

  • @dustinb3403 said in Cross Post - Task scheduler intermittently running a powershell script:

    Why are you passing -noprofile when you are running the process from an administrative account?

    Did you open task scheduler as an administrator and create the process there?

    PowerShell will try to load a profile before running the script.

  • Having TaskScheduler in Windows run PowerShell scritps is like rolling dice depending on the context in which you are running the script...

    So when in doubt:

    I always have guaranteed success when starting the PS script from a batch file first from a scheduled task.

    Create a scheduled tasked using these lines in an elevated powershell window:

    Creates a scheduled task:

    $action = New-ScheduledTaskAction -Execute 'C:\ProgramData\scripts\PSKickoff.bat'
    $trigger = New-ScheduledTaskTrigger -AtStartup
    $principal = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Administrators" -RunLevel Highest
    $settings = New-ScheduledTaskSettingsSet
    $task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -Settings $settings
    Register-ScheduledTask -TaskName "Scheduled Task Friendly but Meaningful Name" -InputObject $task

    Note: Change above New-ScheduledTaskTrigger -AtStartup to match your schedule. Here's the info for it.

    The batch file that the scheduled task runs:

    Powershell.exe -executionpolicy bypass -File "C:\ProgramData\scripts\yourScript.ps1"