Hi sorry for not responding that long ...
Yeah i came accross that article too and i thought the same, would be nice to have a solution tho.
Currently im trying to build a powershell module that will work with Virtualbox and i come accross alot of peculiar bugs,
mostly related to the singular instance of the virtualbox interface itself and or getting instances of sessions that were created by virtualbox.
This module works with virtualbox and many functions work but there are many sideeffects too, mainly because IVirtualbox is implemented as a singleton im guessing.
Also when a session is created by virtualbox it seems impossible to get an instance of that.
This is a bug i came accross today.
Im building a function that will mount a harddrive to the first available port on the SATA Controller.
This function will work as long as the machine and controllerport are created by powershell.
When I Create a machine with the virtualbox manager itself the function will fail !!
This is very weird bug, seems also be related to singleton instances.
Exception calling "AttachDevice" with "5" argument(s): "The port and/or device parameter are out of range: port=1 (must be in range [0, 0]), device=0 (must be in range [0, 0])"
Code: Select all
<#
.Synopsis
Mounts a Harddisk File to a Virtualbox Machine.
.DESCRIPTION
This will mount a harddisk file (eg. vdi, vhd, etc) to the first available port on the SATA Controller.
.EXAMPLE
PS C:\> Mount-VirtualBoxHardDisk -File D:\imagefile.vhd -Name testmachine
.NOTES
If the Machine is made by virtualbox itself we get errors when we try to mount a disk to the second port.
Exception calling "AttachDevice" with "5" argument(s): "The port and/or device parameter are out of range: port=1 (must be in range [0, 0]), device=0 (must be in range [0, 0])"
If we make a virtualbox with the powershell function we dont get this error ....
It evens goes so far that if we create a vbox machine with the ps function but without creating controllerports
and then we add them controllerport (SATA) with the vbox manager itself we get same error ...
#>
Function Mount-VirtualBoxHardDisk {
[CmdletBinding()]
Param(
# Machine Name Parameter
[Parameter(Mandatory=$true,Position=0,HelpMessage="Enter a virtual machine name.")]
[ValidateNotNullorEmpty()]
[String]$Name,
# Harddisk File Path
[Parameter(Mandatory=$true,Position=0,HelpMessage="Enter full path to the harddisk file.")]
[ValidateNotNullorEmpty()]
[String]$File
)
Begin {}
Process {
## Get Machine
Try { $Machine = $VBOX.FindMachine($Name) }
Catch { Return $_.Exception.Message }
## Create Session Object
Try { $Session = New-Object -ComObject VirtualBox.Session }
Catch { Return $_.Exception.Message }
If ($Machine.State -eq $VBE.MachineState.PoweredOff){
## Get Object to Medium
Try {$Medium = $VBOX.OpenMedium($File,$VBE.DeviceType.HardDisk,$VBE.AccessMode.ReadWrite,$false)}
Catch { Return $_.Exception.Message }
## Set Machine Lock Status to Write
Try { $Machine.lockMachine($Session,$VBE.LockType.Write) }
Catch { Return $_.Exception.Message }
##----------------------------------
<# Check if there is an available port#>
## Get all attachments of Sata Controller
Try { $MediumAttachments = $Machine.getMediumAttachmentsOfController("SATA")}
Catch { Return $_.Exception.Message }
## Get available port
[Long]$aPort = $MediumAttachments.Count
## There can be only 30 (0-29)
If ($aPort -eq 30){ Write-Error -Message "No port available for mounting the disk ..."; Return }
## Attach Harddisk to Machine
Try { $Session.machine.attachDevice("SATA",$aPort,0,$VBE.DeviceType.HardDisk,$Medium) }
Catch { Return $_.Exception.Message }
##----------------------------------
## Save Machine Setting
Try{ $Session.Machine.SaveSettings() }
Catch{ Return $_.Exception.Message }
## Unlock Machine
$Session.UnlockMachine()
} Else { Write-Error -Message "Cannot Mount Harddisks to Machines that are Not 'PoweredOff' ... " }
} ## END Process
End {}
}