Automating Disk Zeroing on VM Deletion

A requirement for a project I had was to zero the VMDK of all VM disks at the time of VM removal.

smash_hard_drive

One consideration was to SSH into the host where the VM was located and use vmkfstools like the below on each vmdk to zero the disk.

vmkfstools –w /vmfs/volumes/<…>.vmdk

Looking for alternatives I found that the PowerCLI cmdlet Set-HardDisk has a ZeroOut parameter. Note the text from the help (version 5.8 R1):

Specifies that you want to fill the hard disk with zeros. This parameter is supported only if you are directly connected to an ESX/ESXi host. The ZeroOut functionality is experimental.

The points to note are:

  • You will need to connect PowerCLI directly to the ESXi host that the VM is registered on. So you will most likely first of all need to connect to vCenter to find out where the VM lives.
  • The functionality is ‘experimental’. A quick scan back through releases showed this had been the same for some time. From my observations the functionality appeared to work fine. There have been many things in vSphere over the years which have been ‘experimental’, but have usually worked fine.

So once you have identified where the VM is and connected to the ESXi host in question, it’s a case of simply looping through all of the disks and zeroing them (with a bit of logging thrown in) – note it will likely take a fair amount of time to zero each disk!

$VM = VM01
$HardDisks = Get-HardDisk -VM $VM

foreach ($HardDisk in $HardDisks){

$HardDisk | Set-HardDisk -ZeroOut -Confirm:$false | Out-Null

$Text = "Zeroed disk $($HardDisk.Filename) for VM $VM"
$Text | Out-File -FilePath C:\log\zerodisk.log -Append
}

5 thoughts on “Automating Disk Zeroing on VM Deletion

  1. Wow! Such a small script that accomplishes so much. The only thing I’d want to add is a date stamp on all of the logging messages.

    Well done Jonathan!

  2. The Time stamp would be fairly simple to add

    $VM = VM01
    $HardDisks = Get-HardDisk -VM $VM

    foreach ($HardDisk in $HardDisks){
    $HardDisk | Set-HardDisk -ZeroOut -Confirm:$false | Out-Null
    $time = Get-Date -format -g
    $Text = “Zeroed disk $($HardDisk.Filename) for VM $VM Completed at $time”
    $Text | Out-File -FilePath C:\log\zerodisk.log -Append
    }

  3. We use this routinely and its nice to see this on a site. I’m not sure why this is still lsited in beta by vmware with the demand for it.
    It may seem obvious to state (but I admit I spent a bit of time running down blind alleys) that its worth checking that there are not any current snapshots for the VM or this will fail with an invalidargument error, fullyqualifiedErrorID.
    doh
    You can also do this from a connection to the vcenter rather than to the direct host.

Comments are closed.