As background for this question: we have a Windows Server 2012 R2 Terminal Server where users do long-running work, so we try very hard not to reboot the server unneccessarily. However, sometimes we need to (installing updates being the most important), and this currently requires a lot of communication and manual interference.
This would be implified a lot if the reboot could happen automatically when the server is not in active use. So far my best attempt involved a scheduled task triggered on the event
Security/Security Auditing/4634 (Logoff), but I failed to then determine if the just disconnected session was the last session, and that may not be the best approach anyway.
Is there a way to perform a task (i.e. PS Restart-Computer) as soon as there are no active sessions (interactive or disconnected)?