0

I have a main computer that knows when there is a power outage. That main computer will then tell all workstations to shutdown over the network using the built in "shutdown /m \computername /s" command. That main computer will also tell individual workstations to restart using the same shutdown /m \\computername /r mechanism.

Each workstation has a script that when executed, saves all the important data.

Each workstation has to execute its script when being told to shutdown or restart over the network (shutdown /m \\computername /s or shutdown /m \\computername /r).

Idea1:

  • Use the WM_QUERYENDSESSION event to find out when a shutdown/restart occurs.
  • Cancel the shutdown/restart.
  • Run the script.
  • Continue shutdown/restart (Problem: I don't see a reasonable way to determine if a shutdown or a restart has been issued by the main computer.)

Idea2:

  • Wait until the shutdown.exe process exists.
  • Retrieve the command line parameters of that process
  • Kill that process
  • Run the script.
  • Run shutdown.exe with the retrieved parameters.

Problem: shutdown.exe isn't actually getting executed. Not even if you locally run a simple shutdown -s -t 30.

Idea3:

  • Sniff incoming network traffic.
  • Look for shutdown -m packets
  • ...

Problem: I have no idea what protocol is being used and how the packets are formatted.

Idea4:

  • Monitor event logs
  • Hope that it contains informations about what kind of shutdown has been issued

Problem: I have no idea if and where this would be logged.

PS:
I don't want to use PsExec. The workstations have to execute their scripts on their own.

Forivin
  • 14,780
  • 27
  • 106
  • 199
  • might be better asked in superuser and/or serverfault – Jasen Mar 09 '17 at 21:30
  • My question is pretty specific to programming. – Forivin Mar 09 '17 at 22:03
  • If this is about programming, why don't you program your own shutdown protocol, implemented by a specific .exe installed on all machines, instead of using shutdown.exe ? – Simon Mourier Mar 15 '17 at 04:44
  • Because it has to use the official shutdown /m interface. – Forivin Mar 15 '17 at 11:40
  • Why not combine idea 1 and idea 2? You can figure out if the shutdown was sent by the main computer or not because if it was sent by the main computer, shutdown.exe won't show up. It's a somewhat gross solution, but it may work. edit: this is a comment because I don't know enough about how all of this stuff works to feel confident giving a full answer. – Unlocked Mar 20 '17 at 23:29
  • Why it is impossible to do everything you need in the WM_QUERYENDSESSION handler? (I know why it could be, but what about your case?) – KonstantinL Mar 21 '17 at 07:37
  • Because I wouldn't be able to distinguish between a shutdown and a restart. – Forivin Mar 21 '17 at 14:47

2 Answers2

1

Why doing it harder when you can just set Windows shutdown scripts?

It's pretty easy:

Execute gpedit.msc (local Policies) and then go to: Windows settings -> Scripts -> Shutdown -> Properties -> Add and here add your script. It will execute always shutdown. If you want to run it only if you call shutdown from server side, it will be better to create very simple c# (or other) program to run your script and then shutdown computer. After you have this, you can call it from server.

screenshot

WARNING: The group policy startup and shutdown scipts not executed, when using fastboot (enabled by default in windows 8 and up). In this case, only the restart or force shutdown (from command prompt) shut down really the computer. In all other cases (start menu shutdown), the computer kernel hibernated, and revieved on boot, and GPO startup and shutdown scipts are ignored.

More here.

Edit:

When you have shutdown script and you don't want to create some program, you can check in shutdown script if is some file, what you created exist and if not, then backup and if yes continue normal shutdown and delete this file. When you have this create another script and place it on desktop or anywhere and program it to creating the file and running shutdown.exe, so you will have normal shutdown without backup, but if server call shutdown it will backup.

Community
  • 1
  • 1
Samuel Tulach
  • 1,319
  • 13
  • 38
  • Will such a shutdown script also execute when a system is just going to restart? – Forivin Mar 15 '17 at 20:30
  • @Forivin No only in shutdown. – Samuel Tulach Mar 15 '17 at 20:32
  • Well, I need something that works for both, shutdown and restart. Is it possible to also create a "restart script"? – Forivin Mar 15 '17 at 20:43
  • @Forivin Hmm... now I am looking for some documentation and maybe it will works in restart too, but if it doesn't work you can try adding log off script what will execute on restart, because windows first log off profile: User Configuration -> Windows Settings -> Scripts -> Logon or Logoff -> Properties -> Add – Samuel Tulach Mar 15 '17 at 20:48
0

you can detect Windows shutdown in an always running app in each workstation for example c#.net and before doing shutdown run your script and then continue shutting down. attach an event handler method to the SystemEvents.SessionEnded event. take a look at here

Community
  • 1
  • 1
arman
  • 141
  • 1
  • 11