Solved

Task Scheduler status: currently running. How to stop after Powershell script is finished?

Posted on 2014-01-29
11
12,037 Views
1 Endorsement
Last Modified: 2014-01-30
Hello, I have some scheduled tasks in W2K8r2. At certain times of the day they run PowerShell scripts. Those PowerShell scripts start / stop certain third party Windows services and change the  startup type for those services.

When I check the Task Scheduler Library window the next day, it shows that the status is still set to "Running" and the Last Run Result is set to "The Task is Currently Running."

I know that I can force the scheduled task to stop after a certain time (like one hour) in the Trigger Tab and Settings tab; but I'd rather have the task stop after the script stops.

I'm thinking that maybe the PowerShell scripts are still running. I don't know how to tell.
The Windows Task Manager's Resource Monitor shows that there are four Notepad.exe Images running. Each Notepad.exe is running on one thread. Since each PowerShell script was created in Notepad, maybe that is an indication that the PowerShell scripts are still running.

The Task Manager's Processes tab shows that taskhost.exe (the host process for Windows tasks) is running six threads. Maybe four of those are the PowerShell scripts.

Interestingly, a different scheduled task that runs a batch file has the status as, "The Operation Completed Successfully." On that task, the history shows that the final event was, "Task completed," and the second to last event was, "Action Completed."

So, on the scheduled tasks that start PowerShell scripts and for which the status reads, "Running," the events are (in chronological order)
1. Task triggered on scheduler
2. Task Engine received message to start task
3. Task Started
4. Action Started
5. Created Task Process


However, on the scheduled task that starts a batch file and for which the status reads, "Ready," the events are (in chronological order)
1. Task triggered on scheduler
2. Task Engine received message to start task
3. Task Started
4. Action Started
5. Created Task Process
6. Action completed
7. Task completed.

I want to see the "Action Completed" and "Task Completed" on the tasks that run the PowerShell scripts also.
It seems to me like the PowerShell scripts did what they were supposed to do, but when I check the Windows Application log,  it doesn't show those services starting or stopping as the PowerShell scripts would have had them do.

So, it seems that merely putting an exit command at the end of the PowerShell script is not going to be enough; but should I have that anyways?

I suspect that maybe the scripts don't have the permissions to run PowerShell because I have to open the PowerShell editor with admin rights to get it to run successfully.

Is there a way to get PowerShell to report errors (log, email, etc.) even when run from the Task Scheduler?
Am I thinking about this in the right way, or is there something else I should be looking at?

Here is an example of one of the PowerShell scripts:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

Set-service ArcServerObjectManager -startuptype Automatic
Start-Service ArcServerObjectManager

Open in new window


Thanks in advance
1
Comment
Question by:XTO
  • 8
  • 3
11 Comments
 
LVL 39

Expert Comment

by:footech
ID: 39819281
How is the task configured?  It should be set to run powershell.exe with something like the following for arguments:
-file c:\scripts\ps-script1.ps1
If you just have the powershell script as the item to run (instead of powershell.exe), by default a .PS1 file is associated with Notepad, so that is what will open, it will not run the script.
0
 

Author Comment

by:XTO
ID: 39819375
The Action part of the Task is set to run :
StartService.ps1
That's the name of the file also.
0
 

Author Comment

by:XTO
ID: 39819380
Update, I've tried running the same ps1 file from the PowerShell editor, and it told me that it did not have the correct permissions.

When I ran the PowerShell editor in Admin mode, the ps1 script ran fine.

The Task is set to run with System privileges.

I've changed the ps1 script to this:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

Set-ExecutionPolicy -ExecutionPolicy ByPass -Scope CurrentUser
Start-Service ArcServerObjectManager
Set-service ArcServerObjectManager -startuptype Automatic
Exit

But it still gets the permission denied message if I run it from regular PowerShell, but runs fine if I run it with Admin PowerShell.
0
 

Author Comment

by:XTO
ID: 39819398
Here is the error when I run it from PowerShell without Admin privileges:


Start-Service : Service 'Application Experience (ArcServerObjectManager)' cannot be started due to the following error: Cannot open ArcServerObjectManager service on computer '.'.
At C:\Users\bsekanda\Documents\PowerShell\Script Experiments\startMetricProvider.ps1:4 char:14
+ Start-Service <<<<  ArcServerObjectManager
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],   ServiceCommandException    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand
Set-Service : Service 'Application Experience (ArcServerObjectManager)' cannot be configured due to the following error:

Access is denied

At C:\...\StartService.ps1:5 char:12
+ Set-service <<<<  ArcServerObjectManager -startuptype Automatic
    + CategoryInfo          : PermissionDenied: (System.ServiceProcess.ServiceController:ServiceController) [Set-Service], ServiceCommandException    + FullyQualifiedErrorId : CouldNotSetService,Microsoft.PowerShell.Commands.SetServiceCommand
0
 
LVL 39

Accepted Solution

by:
footech earned 400 total points
ID: 39819607
Your action should be set to run
powershell.exe
with the argument
-file c:\path\startservice.ps1
The task should also have the options checked for "Run whether user is logged on or not" and "Run with highest privileges".  If the account that you're using to run the task is not an admin on the machine, either make it so or add it to the "Log on as batch job" setting in the local security policy (run gpedit.msc to get to it).

You shouldn't need the lines for system.reflection.assembly, set-executionpolicy, or exit.
For the execution policy, if needed either set that from an elevated PS session (the setting will stay after the session is ended), or include the following as an argument for your task (which sets it just for as long as that executable runs).
-executionpolicy bypass
0
Do email signature updates give you a headache?

Constantly trying to correctly format email signatures? Spending all of your time at every user’s desk to make updates? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today!

 

Author Comment

by:XTO
ID: 39820956
re:
Your action should be set to run
powershell.exe
with the argument
-file c:\path\startservice.ps1

Excellent! Thank you. I'm going to try that now.


re:
If the account that you're using to run the task is not an admin on the machine, either make it so . . . .

Yesterday I set the account to System. I have the ability to set it to a domain group name that has admin rights on that machine.  Is that better? Using the System account is not a good idea?

re:
include the following as an argument for your task
-executionpolicy bypass

That should be an argument in the Action tab, not in the code, right?
0
 

Author Comment

by:XTO
ID: 39821028
footech,
That worked.

I entered the argument as:
-file c:\path\startservice.ps1 -executionpolicy bypass

I changed the account name to a group that has admin rights on that machine.

All I need to do now is to see if it runs at the scheduled time tonight.

Thanks very much.
0
 

Author Comment

by:XTO
ID: 39821062
I've requested that this question be closed as follows:

Accepted answer: 0 points for XTO's comment #a39820956

for the following reason:

Whereas when running a batch file I can merely give the path and name of the batch file itself, with PowerShell, I put PowerShell.exe as the program to run, and the path and name of the script are passed in as arguments. That's what I did not know. That's why the task would not stop running.
0
 

Author Closing Comment

by:XTO
ID: 39821063
Whereas when running a batch file I can merely give the path and name of the batch file itself, with PowerShell, I put PowerShell.exe as the program to run, and the path and name of the script are passed in as arguments. That's what I did not know. That's why the task would not stop running
0
 

Author Comment

by:XTO
ID: 39821070
I did not try to have the question closed on purpose. I accidentally clicked on the wrong solution and the site thought that I was trying to close the question. Sorry about that.
I'd appreciate it if the Admins could remove that.
0
 
LVL 39

Expert Comment

by:footech
ID: 39821325
Glad it's working for you now!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article will help you understand what HashTables are and how to use them in PowerShell.
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now