-1

I need to run a powershell script whenever the server is rebooted/shutdown (whether graceful or disgraceful reboot). The script will stop 4 application services at an interval of 1 minute and then finally reboots the system.(This is a business requirement, don't ask why)

How can I make server to invoke the .ps1 script whenever a reboot or a shutdown is initiated.

My test results: I tried to create a test script which will generate a text file with current date/time and added it to the scheduled task on the trigger of event log 6006 (which is created whenever a system reboot/shutdown is initiated.) I checked the box -"Run with highest privileges" but after system restart no text file was generated as it was supposed to, although it generates when ran manually.

Do we have any better approach to implement this?

(My final expectation should look like this- On a random day a random user initiated reboot after a monthly patch when a command prompt window opens before him with message something like:

    Stopping service abc...
    Stopped.
    Waiting for 60 seconds.
    Stopping service xyz... 
    Stopped

EDIT: I've been successfully able to invoke the .ps1 file by adding it to the gpedit as suggested by Kory and Alroc but the script runs only in background when computer restart is initiated. It doesn't opens a regular cmd window to show the progress. I'm adding the .ps1 script as well below which stops 2 services(chosen for testing purpose) at an interval of 10 seconds and will show the timer as well, only when ran manually.When invoked by the shutdown command it'll stop services only in the background without showing the progress to the user. Kindly assist to achieve this?

 Write-Host "Shutdown script invoked"
    stop-service W32Time -force -PassThru
    for($i = 10 ; $i -gt 0 ; $i--)
    {
    Write-Progress -Activity "`n Waiting for" -status "`$i equals $i seconds"
    sleep 1
    }
    stop-service wuauserv -force -PassThru
Brite Roy
  • 425
  • 3
  • 9
  • 28
  • By definition, a "disgraceful reboot" will not execute all of the standard shutdown processes, so you likely will not find a solution that covers that case 100%. – alroc Jan 08 '16 at 02:27
  • no problem, thats the least requirement. First we need to work with Graceful reboots which is going to happen 99% of the times. Any suggestions? – Brite Roy Jan 08 '16 at 02:30
  • 1
    Possible duplicate of [Executing a batch script on Windows shutdown](http://stackoverflow.com/questions/12434863/executing-a-batch-script-on-windows-shutdown) – Kory Gill Jan 08 '16 at 04:21
  • I got an idea from this link . Windows local group policy editor provides a separate tab to add powershell scripts in 'Startup/Shutdown scripts inside gpedit. This worked. Thanks Kory Gill. – Brite Roy Jan 08 '16 at 12:04
  • HI Kory- It's not a duplicate. The provided link only helped the partial reply of my query. I've edited the question to show my progress and final expectation. Kindly remove the duplicacy tag since I'm getting negative votes due to this and let me know if I can achieve what I mentioned in 'Edit'. Thanks in advance. – Brite Roy Jan 09 '16 at 23:22

2 Answers2

2

You can use GPO to configure a shutdown script for systems.

You might be able to to it via a Win32_ComputerShutdownEvent watcher as well.

Community
  • 1
  • 1
alroc
  • 27,574
  • 6
  • 51
  • 97
  • Although I'm curious to use the watcher as well..I'll read about it in google and understand how to use it. – Brite Roy Jan 08 '16 at 12:09
  • Hi Alroc - I'm able to invoke the stopping services script by adding it to the gpedit and whenever system restarts the script is executed only in the background. It doesn't opens the command prompt and shows the user about the progress of stopping services, however it does works when ran manually. How can I make the computer to open a regular cmd window and show script progress whenever system shutdown is initiated. – Brite Roy Jan 09 '16 at 23:04
  • Also I've come to know that the Win32_ComputerShutdownEvent watcher will work only if its added to the logon script which will silently run in the background and will wait all-day for the shutdown command. I can not use this because for that servers will be effective only after they are restarted and logged in Production environment. I've edited my question to show script progress and final expectation. Thanks in advance. – Brite Roy Jan 09 '16 at 23:25
1

After deep digging, I've finally figured out how to make the cmd window visible while system shutdown in progress.

Here is the complete steps of performing above mentioned expectation:

  1. Open gpedit.msc
  2. Navigate to Computer Configuration->Windows Settings->Scripts(Startup/Shutdown)->Shutdown.
  3. Go to Shutdown properties. In the powershell scripts tab add your script and select 'Run Windows Powershell script first'

Above steps will enable the invoke of script at every system shutdown. Now to make the script visible and show progress:

  1. Navigate to Computer Configuration->Administrative Templates->System->Scripts
  2. Among the policies showing in the right pane enable below properties:

    • Run Windows Powershell scripts first at computer start,shutdown
    • Run shutdown scripts visible
Brite Roy
  • 425
  • 3
  • 9
  • 28