Skip to content

Instantly share code, notes, and snippets.

@piotrkochan
Created March 9, 2023 18:48
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 piotrkochan/6f7e40c124a524bfc7886be446b96628 to your computer and use it in GitHub Desktop.
Save piotrkochan/6f7e40c124a524bfc7886be446b96628 to your computer and use it in GitHub Desktop.
start virtualbox hdd shred image with hdd selector
$vm_name = "shred"
$iso_path = "C:\...\shredos-2021.08.2_23_x86-64_0.34_20221231.iso"
$directory = "C:\...\VirtualBox VMs\shred\vmdks"
# Stop the virtual machine if it's running
if ((VBoxManage.exe showvminfo $vm_name | Select-String 'State' | Select-String 'running') -ne $null) {
VBoxManage.exe controlvm $vm_name poweroff -forc
}
# Remove all existing VMDK files from the directory
Get-ChildItem $directory -Filter *.vmdk | Remove-Item -Force
# List available hard drives and allow the user to select one or more drives
$drives = Get-WmiObject Win32_DiskDrive | Select-Object Index, Model, @{Name="Size"; Expression={
$size = $_.Size
if ($size -ge 1TB) {
"$([math]::Round($size/1TB,2)) TB"
}
elseif ($size -ge 1GB) {
"$([math]::Round($size/1GB,2)) GB"
}
else {
"$([math]::Round($size/1MB,2)) MB"
}
}}, MediaType | Out-GridView -Title "Select one or more hard drives" -OutputMode Multiple
if ($drives.Count -eq 0) {
Write-Host "The array is empty. Stopping the script."
return
}
# Remove all existing hard disk attachments from the virtual machine
VBoxManage.exe storagectl $vm_name --name "SATA" --remove
VBoxManage.exe storagectl $vm_name --name "SATA" --add sata --controller IntelAHCI
$port = 0
foreach ($drive in $drives) {
$vmdk_file = "$directory\disk$($drive.Index).vmdk"
# Eject Removable Media
if ($drive.MediaType -eq "Removable Media") {
Write-Host "Ejecting drive $($drive.Index)..."
$drive.Eject()
}
# Remove medium from Media Registry
VBoxManage.exe closemedium disk $vmdk_file
# Create a VMDK file for each selected hard drive
VBoxManage.exe internalcommands createrawvmdk -filename $vmdk_file -rawdisk "\\.\PhysicalDrive$($drive.Index)"
# Attach the new VMDK files to the virtual machine
VBoxManage.exe storageattach $vm_name --storagectl "SATA" --port $port --device 0 --type hdd --medium $vmdk_file
$port++
}
# Attach the bootable ISO to the virtual machine
VBoxManage.exe storageattach $vm_name --storagectl "IDE" --port 0 --device 0 --type dvddrive --medium $iso_path
# Start the virtual machine
VBoxManage.exe startvm $vm_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment