Good morning everyone, I have a small problem regarding a powershell script and scheduled operations.
The script below should stop active processes on a server and restart them and send an email.
If I do it step by step there are no problems everything works correctly if instead I try to insert it a scheduled operation I only get the notification email but the processes don’t stop e don’t restart
If your processes are properly registered, then you shouldn’t have to specify Filepath, but just the name of the process.
Also, are you running it as administrator? If you are, then I’d recommend setting your $Errorpreference to STOP, and wrap it all in Try/Catch blocks to see where it fails, and to see the error.
Maybe I explained myself wrong or google translate has translated incorrectly.
If I manually run my scritp everything works perfectly.
The applications are stopped and restarted without problem and an email is sent to report the restart.
If I try to schedule the .ps1 file then nothing works.
I am only sent the restarted email, the applications are never stopped and restarted.
I did a test trying to run maximized notepad with consequent sending of the email.
The notepad does not start but the email is sent.
The operation is scheduled with an administrative user.
You may want to consider using Start-Transcript and Stop-Transcript in your script (or even in the task setup) to capture everything that’s going on during the execution of the scheduled task. This method allows you actually see what is happening and when.
Add the try/catch as suggested but use the St*-Transcript cmdlets to output to a file.
[quote quote=243134]Maybe I explained myself wrong or google translate has translated incorrectly.
If I manually run my scritp everything works perfectly.
The applications are stopped and restarted without problem and an email is sent to report the restart.
If I try to schedule the .ps1 file then nothing works.
I am only sent the restarted email, the applications are never stopped and restarted.
I did a test trying to run maximized notepad with consequent sending of the email.
The notepad does not start but the email is sent.
The operation is scheduled with an administrative user.
[/quote]
I don’t see anything in your script referring to Notepad?
Is the scheduled task set to run as Administrator? If not, then it will most likely not be able to stop the processes.
If you run it from a normal console, does it work?
If it only works from a console with admin rights, then the scheduled task also needs to run with admin rights.
Also, I always suggest to people to break something that doesn’t work down into each element.
What happens when you follow my suggestion and put each element in a Try/Catch block and log the output?
Thanks for the tips.
The script I created, if run from the powershell console, works correctly.
If I run the .ps1 file it works correctly.
Stops the services that must be stopped, restarts them and sends the email of the restart of the processes.
In the scheduler, the script was run with administrator privileges.
Temporarily I solved the problem by changing the scheduling configurations; instead of running the script, whether the user was logged in or not, I put it into execution only if the user is logged in.
Now works.
I just have to understand how to make it run with or without the logged in user.
Log on as a batch job (Group Policy):
GPMC > Computer Configuration\Windows Settings\Security Settings\Local Policies\User Rights Assignment
I opt for Group Managed Service accounts for scheduled tasks wherever possible as there is literally no management overhead, so no dealing with compromised/expired passwords, etc.
This is obviously going beyond the scope of PowerShell scripting but take a look into these things for your script to start working unattended.