How to schedule a task to run when the windows server is reboot or shutdown?

You cannot use the task scheduler for running scripts on shutdown, but you could instead use shutdown scripts supported by Group Policy. Shutdown scripts are run as Local System.

You can find them under Computer Configuration\Windows Settings\Scripts (Startup/Shutdown)