I must gracefully shutdown some Oracle VirtualBox guests for a nightly backup.
If the guest-os is Linux, first install acpid:
#apt-get install acpid
This is my backup script:
vboxmanage --nologo guestcontrol execute "Guest VM Name" "shutd.bat" --username Administrator --password my_passwd --verbose
#if the host is running on Windows uncomment these lines
#choice /n /t 60 /d 0 /c:0 /m "Waiting 1 min"
#choice /n /t 60 /d 0 /c:0 /m "Waiting 2 min"
#choice /n /t 60 /d 0 /c:0 /m "Waiting 3 min"
#if the host is running on Linux uncomment these lines
#echo "Waiting 1 min"
#sleep 60
#echo "Waiting 2 min"
#sleep 60
#echo "Waiting 2 min"
#sleep 60
#If the first script fails...
VboxManage controlvm "Guest VM Name" acpipowerbutton
#if the host is running on Windows uncomment these lines
#choice /n /t 60 /d 0 /c:0 /m "Waiting 1 min"
#choice /n /t 60 /d 0 /c:0 /m "Waiting 2 min"
#choice /n /t 60 /d 0 /c:0 /m "Waiting 3 min"
#if the host is running on Linux uncomment these lines
#echo "Waiting 1 min"
#sleep 60
#echo "Waiting 2 min"
#sleep 60
#echo "Waiting 2 min"
#sleep 60
#...just to be sure
vboxmanage --nologo controlvm "Guest VM Name" poweroff
#Now do your backup
#Restart the guest-os
vboxmanage --nologo startvm "Guest VM Name"