XTO
asked on
Task Scheduler status: currently running. How to stop after Powershell script is finished?
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:
Thanks in advance
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
Thanks in advance
ASKER
The Action part of the Task is set to run :
StartService.ps1
That's the name of the file also.
StartService.ps1
That's the name of the file also.
ASKER
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.Assembl y]::LoadWi thPartialN ame("Syste m.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.
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.Assembl
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.
ASKER
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\Document s\PowerShe ll\Script Experiments\startMetricPro vider.ps1: 4 char:14
+ Start-Service <<<< ArcServerObjectManager
+ CategoryInfo : OpenError: (System.ServiceProcess.Ser viceContro ller:Servi ceControll er) [Start-Service], ServiceCommandException + FullyQualifiedErrorId : CouldNotStartService,Micro soft.Power Shell.Comm ands.Start ServiceCom mand
Set-Service : Service 'Application Experience (ArcServerObjectManager)' cannot be configured due to the following error:
+ Set-service <<<< ArcServerObjectManager -startuptype Automatic
+ CategoryInfo : PermissionDenied: (System.ServiceProcess.Ser viceContro ller:Servi ceControll er) [Set-Service], ServiceCommandException + FullyQualifiedErrorId : CouldNotSetService,Microso ft.PowerSh ell.Comman ds.SetServ iceCommand
Start-Service : Service 'Application Experience (ArcServerObjectManager)' cannot be started due to the following error: Cannot open ArcServerObjectManager service on computer '.'.
At C:\Users\bsekanda\Document
+ Start-Service <<<< ArcServerObjectManager
+ CategoryInfo : OpenError: (System.ServiceProcess.Ser
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.Ser
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
ASKER
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.
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.
ASKER
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.
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.
ASKER
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
ASKER
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.
I'd appreciate it if the Admins could remove that.
Glad it's working for you now!
-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.