Turning it off should be possible by using the adb command @Peter Simpson suggested in his post. It could probably be done, without USB connection, also by using Accessibility Services.
Turning it on is probably very difficult unless there are some pre-installed binaries on the phone. Found this (not tested by me though):
If you enabled USB debugging and installed Minimal ADB & Fastboot before the phone turned off, go to your computer and launch the tool. Plug in your phone via USB, then type adb reboot and hit enter. Also try adb reboot recovery. This will not work for all phones when they are switched off, but it is worth a try.