I was having issues shutting down another user’s Virtualbox virtual machines. Specifically, my virtual machines run as a particular user, but my UPS software’s shutdown script runs as root. Root cannot find any VMs registered to it.

I combined instructions for how to execute as another user and instructions for how to shutdown all running machines:

Note that the former is a poor solution in its own right as the VBoxManage command in my experience is blocking anyway, and if you have other VMs that you don’t shut down, the loop will never end!

My combined solution is:

su -c "vboxmanage list runningvms | sed -r 's/.*\{(.*)\}/\1/' \
| xargs -L1 -I {} VBoxManage controlvm {} savestate" -s \
/bin/bash username

Substitute in the name of the user under which the VMs are running. Repeat for each user if needed.