Re-Add thin provisioned disks via PowerCLI

Running out of disk space in your datastores with those thin provisioned disks? The current shop I am in went with thin provisioned disk, and of course it is now catching up with them. They currently use a RIS (remote installation service) to re-image the virtual machines. They don’t store any information on these virtual machines, so a re-image is suitable for they’re purposes. I would have gone with non-persistent disks in this case, that way changes are discarded after a reboot. But, rules in a government shop don’t change that easily.

Let me say first that this script removes the current hard drive and re-adds it. This new drive will not have an OS, it is just a blank disk. But if you have a remote installation service, the drive would be reloaded. The old hard disk stays on your datastore, so you have to go back and delete it if you do not want it anymore. It looks something like this:

foreach($vm in (Get-VM -Name “VMname*”)){

Stop-VM -VM $vm -Confirm:$false

Get-HardDisk -VM $vm | Remove-HardDisk -Confirm:$false

New-HardDisk -VM $vm -CapacityKB (20 * 1MB) -ThinProvisioned:$true -Confirm:$false

Start-VM -VM $vm

}

The VMname* (with the wildcard) would be for instance if you had a group of VM’s with the name: VMname01, VMname02 and so on.

If you needed to call a certain set of VM’s, you would want to use

Import-Csv “C:NewDisks.csv” | %{ $vm = Get-VM $_.Name

Instead of “foreach($vm in (Get-VM -Name “VMname*”)){“

Remember though, this removes your current hard drive and adds a blank one. This script will be useful if you need to remove and re-add those thin provisioned disks in an environment where you have a remote installation service.

Now, a more direct and destructive way is to delete the old hard disk in the process. Make sure you no longer have a need for the old hard disk before you use this one. There is no “delete hard disk” function, only a “remove-harddisk” function. So we need to create it.

function remove-HD {
param($VMname, $HDname, $Delflag, $Server)

if(!$Server){
$server = $defaultVIServer
}

$vm = Get-View -Server $server -ViewType VirtualMachine -Filter @{“Name”=$VMname}

foreach($dev in $vm.Config.Hardware.Device){
if ($dev.DeviceInfo.Label -eq $HDname){
$key = $dev.Key
$name = $dev.Backing.FileName
$disktype = $dev.Backing.GetType().Name
}
}

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.deviceChange = @()
$spec.deviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDevice
$spec.deviceChange[0].device.key = $key
$spec.deviceChange[0].operation = “remove”

$vm.ReconfigVM($spec)

if ($Delflag){
$serviceInstance = get-view ServiceInstance -Server $Server

$fileMgr = Get-View $serviceInstance.Content.fileManager
$datacenter = (Get-View (Get-VM $VMname -Server $Server | Get-Datacenter).ID).get_MoRef()

$fileMgr.DeleteDatastoreFile_Task($name, $datacenter)
switch($disktype){
“VirtualDiskRawDiskMappingVer1BackingInfo” {
$ext = “-rdmp.vmdk”
}
“VirtualDiskFlatVer2BackingInfo” {
$ext = “-flat.vmdk”
}
}
$name2 = $name.substring(0,$name.length – 5) + $ext
$fileMgr.DeleteDatastoreFile($name2, $datacenter)
}
}

foreach($vm in (Get-VM -Name “YourVMHere**”)){
Stop-VM -VM $vm -Confirm:$false
remove-HD $vm.Name “Hard disk 1” $true
New-HardDisk -VM $vm -CapacityKB (20 * 1MB) -ThinProvisioned:$true -Confirm:$false
Start-VM -VM $vm
}

Of course there are some great products on the market that help deal with virtual disks and have some added bonuses.

Diskeeper now has V-Locity

Quest has vOptimizer

If you know of more, just list them in the comments section of this post.

You could also use a V to V process with VMware converter, but that would be a pain.

Of course the easiest solution for this would be to just use templates to deploy new VM’s, but if you are in a government shop like mine where change doesn’t come easy, these scripts will help out.

Thanks again to LucD for helping me out.

One thought on “Re-Add thin provisioned disks via PowerCLI

  1. Just wish to say your article is as astonishing. The clarity in your post is simply cool and i could assume you’re an expert on this subject. Well with your permission let me to grab your RSS feed to keep up to date with forthcoming post. Thanks a million and please carry on the enjoyable work.

Leave a Reply

Your email address will not be published. Required fields are marked *