Windows 2012: how to make power button work in every cases?

Solution 1:

The fact you can't power down a 2012 server when a session is locked is probably by design. I faced a similar need and don't know any policy or registry change to allow that.

I used a simple trick: I captured the power button event which simply run my favorite shutdown.exe with the desired parameters after a timeout.
If you use the correct shutdown parameters, you won't be blocked by open programs.

To capture events, just open the task scheduler and create a task triggered on the desired event ID.

Solution 2:

Check out AutoEndTasks:

You can set it for all users by creating the key here vs HKCU: HKEY_USERS.DEFAULT\Control Panel\Desktop

Additionally, there is some tuning that can be done via group policy that can be found here: Computer Configuration>Administrative Templates>System>Shutdown Options> Turn off automatic termination of applications that block or cancel shutdown

Additional tuning options that you may want to look into are: WaitToKillServiceTimeout,WaitToKillAppTimeout,HungAppTimeOut

I'll also second what MarcoZink stated, check your Bios and verify there isn't any special setting configured for your power button that may be holding things up.