We'd need to see some kind of feedback on the error condition that happens, perhaps with text output of the commands and the 'std err' stream redirected to a text file. Web-search how to redirect std-out and std-err to text files.
FWIW I too have had times where a VM refused to turn off, though it was not while save-stating or running headless. I had to run a timer in the batch file that tested for the VM's state to be powered off, and upon expiration of the time, would run a 'controlvm poweroff', which would kill the stuck VM.
However, I really feel a Stack-Exchange-style 'frame challenge' is required. Have you tested restoring your backups? Because I strongly suspect that you're not getting what you think you're backing up.
lazna wrote:backup script ...
vboxmanage.exe controlvm "machine" savestate
A saved state can only be reliably restarted on the original host with the original version of Virtualbox that was running the VM when it was save-stated. The saved state includes processor states, so only the original CPU can run it, and saved states cannot survive across Virtualbox version changes. The only way to get the saved-state VM to restart without original hardware and Virtualbox is to discard the saved state, which loses all unsaved data in the VM and drops the VM to the same as if it were powered off unceremoniously, with dirty databases, at which point 'controlvm poweroff' would be faster.
lazna wrote:vboxmanage.exe controlvm "machine" savestate
...
vboxmanage.exe export "machine"
I just tried exporting and re-importing a saved-state VM. It did not import as a saved-state VM, but as a powered-off VM instead. The saved state was missing. I don't always use export/import, so I may have missed something, but I don't think saved state is compatible with export/import.
Export is not recommended for backups, because the exported OVA is not a bit-for-bit copy of the original and cannot be file-compare checked to see if the backup is a good backup. Also, some UUIDs of the VM might change, which might cause software activation issues.
The best backup is a folder copy of a fully and peacefully shut-down not save-stated VM's folder, along with any disk files stored outside the VM folder. If the disk file is inside the VM folder next to the VM's .vbox file, the backup copy of such a folder can be copied and started on any capable Virtualbox host. This backup is not locked to a particular host or version of Virtualbox like a saved state is, and it can be file-compared to confirm backup integrity.