powershell and Scheduled Task

Welcome Forums General PowerShell Q&A powershell and Scheduled Task

Viewing 7 reply threads
  • Author
    Posts
    • #243125
      Participant
      Topics: 2
      Replies: 2
      Points: 17
      Rank: Member

      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

      [/crayon]

      Would anyone know how to make suggestions?

      I can’t understand why the processes are not stopped and restarted and the email is sent being part of the same code.

      Thanks everyone for your attention

    • #243128
      Participant
      Topics: 13
      Replies: 40
      Points: 277
      Helping Hand
      Rank: Contributor

      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.

       

    • #243134
      Participant
      Topics: 2
      Replies: 2
      Points: 17
      Rank: Member

      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.

      • This reply was modified 2 months, 1 week ago by Bruso7758.
    • #243158
      Participant
      Topics: 7
      Replies: 124
      Points: 600
      Helping Hand
      Rank: Major Contributor

      Can you share the details of your Scheduled Task?

    • #243164
      Participant
      Topics: 11
      Replies: 127
      Points: 769
      Helping Hand
      Rank: Major Contributor

      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.

    • #243182
      Participant
      Topics: 13
      Replies: 40
      Points: 277
      Helping Hand
      Rank: Contributor

      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.

      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?

    • #244079
      Participant
      Topics: 2
      Replies: 2
      Points: 17
      Rank: Member

      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.

      Meanwhile the script has been revised a bit.

      [/crayon]

       

    • #244100
      Participant
      Topics: 11
      Replies: 127
      Points: 769
      Helping Hand
      Rank: Major Contributor

      Are you opening Task Scheduler as Administrator when setting up the task? Does that user account have the Log on as a batch job user right?

      Log on as a batch job (Local Security Policy):
      https://www.brooksnet.com/faq/granting-logon-as-batch-privilege

      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.

Viewing 7 reply threads
  • You must be logged in to reply to this topic.