Scheduled Task – Powershell Starts, but does not complete

Background

Scheduled a Task, but it is not completing.

 

Task Scheduler

Let us review the Task…

Task Overview

 

Task – Action

TroubleShooting

Task Manager

Launched Task Manager and looked for the Task.

One pointed to do so is to look at Command Line Column.

Image

 

Findings

  1. Command Line
    • C:\Windows\System32\notepad.exe “E:\Scripts\Service\ServiceMgmt\serviceStart.ps1”

 

Interpretation

It seems that ps1 files are attached to notepad.exe

 

Why Notepad?

 

Jacob Zinicola // How To Geek
How to Configure Windows to Work with PowerShell Scripts More Easily
Link

PowerShell is not associated to the .PS1 file extension by default.
Windows sets the default action for .PS1 files to open them in Notepad, instead of sending them to the PowerShell command interpreter.
This is to directly prevent accidental execution of malicious scripts when they’re simply double-clicked.

 

Workaround?

Associate PS1 with Powershell.exe

It is easy enough to associate ps1 files with Powershell.exe and get PS1 files to run directly from the command line.

 

Scheduled Task

But, even after this change, was still unable to get PS1 to run directly as a scheduled task.

 

True Solution

The only true solution is the one where we  a write a command file and invoke the PS1 script in the cmd file…


set "_app=c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
set "_PSExecutionPolicy=bypass"

set "_currentFolder=%cd%"
set "_script=serviceStart.ps1"
set "_scriptFullName=%cd%\%_script%"

%_app% -NoProfile -Executionpolicy %_PSExecutionPolicy% -file %_scriptFullName%

 

References

  1. Warren Frame // Cookie Monster
    • Troubleshooting PowerShell Based Scheduled Tasks
      Link
  2. Stack Overflow
    • Powershell script does not run via Scheduled Tasks
      Link
  3. Weekend Scripter: Use the Windows Task Scheduler to Run a Windows PowerShell Script
    Link
  4. How to Geek
    • Jacob Zinicola
      • How to Configure Windows to Work with PowerShell Scripts More Easily
        Link

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s