Skip to content

Instantly share code, notes, and snippets.

@crossan007
Created May 31, 2018 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crossan007/2fe4a459936616705ae9e71447293f8d to your computer and use it in GitHub Desktop.
Save crossan007/2fe4a459936616705ae9e71447293f8d to your computer and use it in GitHub Desktop.
VirtualBox Resize Disk on VM
$VMName = "MyCoolVM"
$NewDiskSize = 102400
$VBoxManage = 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe'
# Gather data from VBoxManage
$VBOXDrive = & $VBoxManage showvminfo $VMName | Select-String ".vmdk"
$RegEx = 'SATA \((\d),\s?(\d)\):\s?(.*?)\s\(UUID:(.*?)\)'
$M = [System.Text.RegularExpressions.Regex]::Matches($VBOXDrive,$RegEx)
$VMDK = $M.Groups[3].Value
$fi = [System.IO.FileInfo]::new($VMDK)
$newFile = [System.IO.Path]::Combine($fi.DirectoryName,"$($fi.basename).vdi")
#Clone the VHD
& $VBoxManage clonehd $VMDK $NewFile --format vdi
# Resize the clone
& $VBoxManage modifyhd $NewFile --resize $NewDiskSize
#Find the storage controller
$VBOXController = & $VBoxManage showvminfo $VMName | Select-String "Storage Controller Name"
$RegEx = 'Storage Controller Name \(0\):\s*(.*?)$'
$M = [System.Text.RegularExpressions.Regex]::Matches($VBOXController,$RegEx)
$VBOXControllerName = $M.Groups[1].Value
#Attach new
& $VBoxManage storageattach $VMName --storagectl $VBOXControllerName --port 0 --device 0 --type hdd --medium $NewFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment