Service Recovery "Run a Program" not working - windows

Unable to fire command (or file) when a service crashes/fails.
I have a script that I can run manually that emails me event logs that match search criteria. When I set said service to run a program using the following parameters:
Program:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Command line Parameter:
-ExecutionPolicy Bypass -File C:\PowerShell_Scripts\tftp_restarttftpd_restart.ps1
I've also tried just echoing some text to a file as well as send-mailmessage in Command Line Parameter. I've also used the -Command suffix. None work.
I trigger the service failure buy capturing the PID and running taskkill against it. I see the service fail but no email triggers.
I've looked for events to indicate the script was at least attempted but I don't see anything.
Help appreciated.

Related

Running PowerShell in Task Scheduler

I am using PowerShell for downloading data from email.
I want to run this process by PowerShell. When I run script like this:
D:\script.ps1
in powershell.exe it works fine.
When I schedule it in Task Scheduler nothing happens.
I tried it to Set it like Program/script:
powershell
Powershell.exe
powershell.exe
Add arguments:
-executionpolicy bypass -file D:\script.ps1
-file D:\script.ps1
-file "D:\script.ps1"
And nothing works. I'm using Windows 2008 R2.
Troubleshooting scheduled tasks is a pain in the rear, because you can't really see what's going on. These are some things you may want to check:
Check that your commandline works in principle, e.g. by running it from CMD (in your case try running powershell.exe -File "D:\script.ps1"). If that gives you any errors you need to fix those first.
If you intend to run the task as a particular user, start CMD as that user and run the same commandline to check if the user has all the permissions required for whatever the script is doing.
Check if your task actually terminated or if the process is still running (via Process Explorer, Get-Process, Task Manager, …).
Check the Last Run Result for the exit code of the command.
Enable the history for your scheduled tasks (Action → Enable All Tasks History). That will give you at least some information about what the task is doing, whether it starts at all, and if/which errors occurred. You need administrative rights to enable the task history.
Check the eventlog for errors/warnings correlating with the task run.
Add logging statements to the script you're running to record progress information. Personally I prefer logging to the eventlog, because that avoids filesystem permissions issues.
Write-EventLog -LogName Application -Source EventSystem -EventID 100 -EntryType Information -Message 'Your log message.'
If you have admin privileges on the system you can register an event source of your own and use that in the above log statement instead of abusing an existing source like EventSystem:
New-EventLog -Source MyEventSource -LogName Application
Further help will depend heavily on the findings you got following these steps as well as your actual script code.
I found this site that was quite useful:
http://www.microsoftpro.nl/2011/07/07/how-to-schedule-a-powershell-script-using-scheduled-tasks-in-windows-server-2008-r2/
I also changed Secure option property and it helped.
I didnt check: Do not store password and now it runs without me being logged into network.
Peace
Few major observations which I had faced:
Instead of giving only powershell.exe , try giving the full PS path C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.
Permission is one more concern. The user through which you are running the task might not have the permission to run that.
Execution Policy: Make sure you are bypassing the execution policy using -ExecutionPolicy Bypass.
Make sure you are running the task with Highest Privileges.
Finally, through analysis of logs.

Task Scheduler to run a batch file to call a powershell script

I have created a Powershell script that I call from a batch file, and everything works fine when I call the batch file. The problem I am running into is I need to set the batch file to run in Task Scheduler. It starts fine, but it keeps hanging up because the task scheduler never says "The operation completed successfully" (0x0). Instead, it stays at "The task is currently running" (0x41301). Please advise, and I understand this is not the most ideal way to call a Powershell Script but for our environment and limited knowledge of scripting it works the best for us.
You should just use Task Scheduler to run PowerShell.
Create a new task, go to Actions tab, then choose New..., and inside this new window, you can run any program, like you run something from cmd.
Inside Program/script square, you simply put Powershell.exe, and inside Add arguments (optional) powershell arguments. This will work the same, as you would type in normal command line:
powershell <arguments>
So if you want to run script, that is saved in your disk, simply put this in arguments list:
C:\LocalisationOfScript\script.ps1 "argument 1" argument2
If you want more options, just add common parameters before this:
-windowstyle hidden -executionpolicy bypass C:\LocalisationOfScript\script.ps1 "argument 1" argument2
Of even this:
-windowstyle hidden -executionpolicy bypass if (Test-path C:\script\script.ps1) { C:\script\script.ps1 "argument 1" argument2 } else { return -1 }
And finally:
Start-Process Powershell.exe -argumentslist "-a -b -c copy" -windowstyle hidden -wait -erroraction stop
You can even add try, catch to last example.
Thank you for all the comments, i researched you advice and came across the exit command i forgot to add to the end of my script, so when i call my script it left the session to exchange open after i applied the exit command to the end of the script the program has been running without error (knock on wood) sense the fix and after i closed and reopened Task Scheduler the last run message changed to (0x0)

Scheduled task & PowerShell Script not being executed

I know this has been asked a 1000 times and I think I looked through all of them.
I have scheduled tasks running PowerShell Scripts on other servers already, but not on this server. Which has me scratching my head as to why I can't get it to work on this server.
I have a powershell script on a Windows 2008 R2 server. I can run it manually and it all works perfectly, but when I try to run it from a scheduled task the History says it was run, but the PowerShell script does not execute.
PSRemoting is enabled
The server ExecutionPolicy is "RemoteSigned"
I get two entries in the History
Action completed
Task Scheduler successfully completed task "\Processing" , instance "{dbbd4924-42d6-4024-a8ed-77494c7f84cf}" , action "C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.EXE" with return code 0.
Task complted
Task Scheduler successfully finished "{dbbd4924-42d6-4024-a8ed-77494c7f84cf}" instance of the "\Processing" task for user "domain\user".
The Scheduled Task looks like this:
I set to run under my account while I'm logged on. (since I can run the script manually as myself already)
checked Run with highest privileges.
trigger is to run every 10 minutes
Start a program Action.... Powershell.exe
Arguments: -executionpolicy remotesigned -File D:\abc\def\powershell\Processing.ps1
Conditions & Settings default settings.
Ensure that you're not being blocked by a permission issue with the task:
http://blogs.technet.com/b/askperf/archive/2012/04/18/task-scheduler-error-a-specified-logon-session-does-not-exist.aspx
The above GPO prevents credentials from being saved. Other User Rights Assignment settings can prevent things being run as batch/script/task/etc.
As a workaround, you can also set the task to run a .bat file with the powershell task. Adding an echo or pipe parameter may give you some clues to the issue.
I would recommend that you should add some diagnostic logging to this script to find out a place that causes this issue or redirect output of this script to a file. E.g. change your string with arguments this way:
-executionpolicy remotesigned -File D:\abc\def\powershell\Processing.ps1 2>&1 d:\output.log
Looks like that the script is executed, but something goes wrong. There are too many reasons for such behavior and it is difficult to find the root cause without a code.

powershell running as windows service giving incomplete output

I want to run a powershell script as windows service. The script is pretty huge and takes a while to run.
I am redirecting the output of this script as text using start-stop transcript in powershell script. When the script is ran through windows service output is not complete. Its giving me half the output.
I have created service using :
"C:\path\to\instsrv.exe" myservice "C:\path\to\srvany.exe" then giving
C:\path\to\powershell.exe -noprofile -File "C:\path\to\myscript.ps1" in Application in registry.
While running script through powershell console, transcript redirects full output to a file but not through service.
Any help will be much appreciated.

Task Scheduler Action parameter should generate log, but doesn't

As the title suggests, I have an added parameter in my Task Scheduler Actions that logs stdout and stderr to a log.txt file. The logging works when the action is run through the command prompt, but not when the action is run by the actual Task Scheduler (at its specified time). Task scheduler reports the action runs successfully, but I can't be sure it does because there's no logging:)
Command looks like this
powershell.exe -file "D:\Scripts\TimeSync2.ps1" > "D:\Scripts\timeSync_log.txt" 2>&1
I'm unfortunately not a native Windows user, so any help would be appreciated. I'm running Windows Server 2008 R2 Enterprise.
Thanks!
Cmd.exe handles command redirection. You have to run it under cmd.exe. Powershell probably also can do redirection but in your script (.NET can).
A black window just means a console program is running. Only if cmd is running does cmd features become available. By starting cmd or by putting it in a batch you canget redirection from cmd.
cmd /c powershell.exe -file "D:\Scripts\TimeSync2.ps1" > "D:\Scripts\timeSync_log.txt" 2>&1
See for Help
cmd /?
Place the command you listed in a batch file and then schedule the batch file.
If you are doing so and it fails, then try it with your account credentials as authentication in task scheduler, to see if it is a permissions issue.
#echo off
powershell.exe -file "D:\Scripts\TimeSync2.ps1" > "D:\Scripts\timeSync_log.txt" 2>&1

Resources