dimanche, août 14, 2011

Creating and starting VirtualBox machine with PowerShell

BSD Router Project provided a VBScript for starting MS Windows based virtualbox lab. But because this vbs script works only on XP, I had to found a more powerful solution: Why not discovering PowerShell and using the VirtualBox COM API ?
I didn't found very detailed examples, then here is a PowerShell script that explains how to create, configure and start a VirtualBox Machine.
The goal is:
  1. Create a VM (OS type: FreeBSD_64)
  2. Configure it (with a serial port that redirect to a pipe)
  3. Convert a unzipped BSDRP-full-amd64-vga disk image file to a VDI file, and attach this VDI to the VM
  4. Start the VM
And here are the big steps of this script:
  1. Initialize the COM API
  2. Create the VM
  3. Configure the VM (excluding attaching medium to it)
  4. Save and register the VM
  5. Convert a RAW file to VDI (using VBoxManage.exe) and registering it
  6. Lock the VM in write mode for attaching the medium
  7. Launch the VM
Now the PowerShell code sample: