Windows Server 2012 Scheduled Tasks Are Not Launching PowerShell Scripts Properly

Problem scenario:  For no apparent reason, Scheduled Tasks are not kicking off PowerShell scripts.  You have configured Scheduled Tasks to launch .ps1 files.The task calls "%windir%\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe" (in the Action tab's "Program/Script" field) with an argument like this "C:\Program Files\good script.ps"

You manually run the scheduled task (by right clicking its name and choosing Run) and the Scheduled Task will seem to run.  In reality, the .ps1 file will not execute (despite having the correct Execution Policy).  You allow the time to pass when it is scheduled to run.  Still there is no evidence that the job runs.  When you look at the Scheduled Tasks you see a column for "Last Scan Result."  Underneath it you see things like this for the Scheduled Task that never really runs:  "The operation completed successfully" or "0x1".  There are no other hints in the history of whether or not previous runs were successful or not.

Root Cause:  The argument has spaces in it.  There is a space between "Program" and "Files".  There is a space between "good" and "script".

Solution:  Change the location of the .ps1 file to not be in a "Program Files" subdirectory.  The path must have no spaces in it.  Change the name of the .ps1 file to eliminate the space.  Reconfigure the Scheduled Task to have the Action tab's argument field point to the new name and location of the .ps1 file.

Miscellaneous:  Microsoft should have a special parsing feature in PowerShell when this command and flag are ran "schtasks /tr".  In the GUI for Task Scheduler, the argument field should detect spaces and not allow the administrator to leave a space in there.  Spaces can exist in the "Program/Script" field of the action of the Scheduled Task.  But the argument cannot have spaces.  If the argument is encapsulated in single quotes and there is a space in the file name, the "Last Scan Result" can speciously say "The operation completed successfully."  If the argument has double quotes or not quotes with a space in its name, the "Last Scan Result" will show "0x1" while the history will not clearly explain what is wrong.

Leave a comment

Your email address will not be published. Required fields are marked *