Running VBoxManage.exe from Windows Task Scheduler

I have been trying to run VBoxManage.exe from the Windows Task Scheduler to periodically list vms. The problem I had was that it wouldn’t list the VM’s I had registered, even if the task was run as the user that created the VM’s and with the ‘Run with highest privileges’ selected.

When searching the Internet, I saw more people having difficulties running it this way, so I started experimenting. Using ‘psexec‘ and ‘runas’ (use the /savecred option and run it once manually from command line) did work when running the VBoxManage.exe as the VM’s creator user while being logged as someone else, but running it from the Task Scheduler still did not work.

When listing (or starting or stopping), it is looking for the VirtualBox.xml file in the .VirtualBox directory stored in the user’s profile directory. A tip from the VirtualBox forums was running “vboxmanage registervm” from the Task Scheduler before listing it… yes, that works! Now the VM was listed, but only that one was registered. Running it twice will give an error that it ‘has the same UUID as an existing virtual machine’, so it must have been stored somewhere in a VirtualBox.xml file. Searching the complete machine only gave me the one from my own profile with all the VM’s, not the one just registered… where is this VM stored and and which VirtualBox.xml?

It’s VBoxSVC.exe !!!

I used ‘procmon‘ to figure out what was called all the way. I filtered VBoxManage.exe from the Process Names, but no VirtualBox.xml called… so I cleared the filter… and there is was, VirtualBox.xml is called… from VBoxSVC.exe and started shortly as a service and disappears afterwards (use ‘procexp‘). It’s VBoxSVC.exe which is not run as a user, but always as the system account and it’s home directory is… ‘C:\Windows\System32\config\systemprofile‘. There is the .VirtualBox directory and VirtualBox.xml. Hidden from the Windows search!

VBoxSVC.exe in procmon

VBoxSVC.exe in procmon

Because VBoxSVC.exe called from the VBoxManage.exe process (I think, but it’s tree originates from wininit.exe / services.exe / svchost.exe), it’s seems not possible to start VBoxSVC.exe as an other user. I have tried to install the ‘psexec service’ or create a service with ‘sc’ and run it as service under the VM’s creator user, but it won’t stay alive (you can see it’s using the right user directory now) and I’m not even sure VBoxManage.exe will fall for it.

Make a junction

I got it running, I created a ‘junction‘ (a directory symlink for NTFS) to the .VirtualBox directory from C:\Windows\System32\config\systemprofile to my own user directory:

c:
cd c:\Windows\System32\config\systemprofile\
rename .VirtualBox _.VirtualBox
junction .VirtualBox C:\Users\Airell\.VirtualBox

Task scheduler ‘run as’ with highest privilege

When running the script with VBoxManage.exe from the Task Scheduler:

Run as with highest privilege

Run as with highest privilege

  • run it as the user who created the VM’s
  • select ‘Run task with highest privileges’ (without this option, the VBoxSVC.exe is not started)
  • –> make sure VirtualBox.exe is not running, this will lock the VirtualBox.xml !

Finally, my list of VM’s are now listed and can be stopped and started from the Windows Task Scheduler!

Sysinternals

I prefer working with Linux, but when running into trouble on Windows, I always use the tools from Sysinternals.

Happy VM Tasking!

Tagged . Bookmark the permalink.

6 Responses to Running VBoxManage.exe from Windows Task Scheduler

  1. Ben says:

    Great solution Ian! This got me where I wanted to be scheduling my headless VM. Impressive use of the Sysinternals Suite!

    I was able to check “Do not store password” and leave “Run with highest privileges” unchecked as well.

    • Ian Hoogeboom says:

      Hello Ben,

      Thank you for you comment, great to hear it works!
      I will try leaving those to options unchecked, but I’m stuck at the moment because I can’t get Wake-on-Lan working when VirtualBox is installed.
      The VirtualBox Bridged Network Driver ‘filter’ keeps my PC from waking on magic packages. When de-installing this, WOL works.
      My idea was to have my PC sleeping when no VM’s are running, wake it to start VM’s, and make it sleep when the VM’s are shut down…

      Cheers, Ian.

  2. Pat says:

    Well, I am trying to do this in Dec 2016 with VirtualBox 5.1 and am unable to get this working.
    Did the junction thing, check it, even applied Everyone full control, did set the VBOX_USER_HOME environment variable.
    But when I run
    VBoxManage list runningvms > %TEMP%\runningvms.txt
    I just get an empty file

    • Ian Hoogeboom says:

      Hi Patrick,

      Sorry, with 5.1 I get the same issue.
      ‘VBoxManage list vms’ does list all my Virtual Box VMs, but also my ‘VBoxManage list runningvms’ is empty too :(
      Even if I run it normally as a OS user I don’t see started VMs.
      First this needs to get fixed…

      Regards, Ian.

  3. foo says:

    Just to clarify, I’m starting a VM headless on a Windows Server 2016 host using the Task Scheduler (it starts the VM on system startup, before a user logs on). I then logon (with the same user that I used in Task Scheduler) and when I do “VBoxManage list runningvms” no running VMs are listed, although the VM is definitely running.

Leave a Reply

Your email address will not be published. Required fields are marked *