VMware Cloud Community
DZ1
Hot Shot
Hot Shot

I was so happy when the script was running, but I made a mistake

I wrote a nice short script to move any VMs that had a thick disk format to a datastore and make it thin.  Here is what I have:

#Simple line, just getting the VMs with a thick HD and saving it to a variable

$thickVMs = Get-VM | where { ($_ | Get-HardDisk).storageformat -eq "Thick" }

 

#Here is what I needed.  Because we have different datacenters, I needed to make sure the VM was getting moved to a datastore within its own datacenter, so for the current VM, I retrieved it's current host, and the datastores that it sees.  Then I had

#to make sure that the datastore wasn't one named "ISOs", and that it wasn't the current datastore where the VM is now.  Then I needed to choose the datastore with the most freespace, and move the VM there and make it thin.

foreach  ($VM in $thickVMs) {

Move-VM -VM $VM -Datastore ( $vm | Get-VMHost | Get-Datastore | where { ($_.name) -ne "ISOs" -and $_.name -ne ($vm | Get-Datastore) } | sort FreespaceGB -Descending | select -First 1) -DiskStorageFormat Thin

}

I tested it out with some small test VMs, and everything appeared to work, but when I ran the script, and this was hours later, what I found out was that everything was going to 1 datastore.  The "sort FreespaceGB -Descending | select -First 1" section isn't changing to a new datastore that has more space, it only selected the first one that had more space, and VMs were moved there, and the datastore almost filled up. 

I just don't understand why this happened.  I thought that it would loop to each new VM, get the current datastore with the most space, and dynamically change as the datastore freespace changed.  What am I missing? 

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership

Are you sure the datastore objects (returned by Get-Datastore) have a FreeSpaceGB property ?

That property was added not that long ago.

Or does that first selected datastore has so much free space that it will always be selected for the first X svMotions ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot

Thanks for responding LucD,

Yes, there is a FreespaceGB property, I tested it out a piece at a time, I also see the...I guess old FreespaceMB one.  I can see how initially the one particular datastore may have been selected over and over, but then it only had about 73GB free, and there were datastores that had easily over 800GB free, when the script was still putting everything on that one particular one.  I still have the error messages up showing "Insufficient disk space on datastore 'name'", and I can see that at least 5 VMs tried to get moved there, even though it didn't have the space to support them.  It just looks like it should work correctly. 

Reply
0 Kudos
LucD
Leadership
Leadership

What does this return ?


foreach  ($VM in $thickVMs) {

          $ds = Get-VMHost -VM $vm | Get-Datastore | where { ($_.name) -ne "ISOs" -and ($vm | Get-Datastore | %{$_.Name}) -notcontains $_.Name} |

                   sort FreespaceGB -Descending | select -First 1 | Select -ExpandProperty Name

          $vm | Select Name,@{N="Datastore";E={$ds}}

}



Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot

I don't understand what's going on.  I ran that script, but the VMs are only matching up with two datastores.  I thought that maybe the sort -descending was wrong, or that it somehow appears differently if you sort from a host or from the datacenter.  We have over 10 datastores, and it's pulling from the datastore that has the lowest freespace, and one that has the 3rd to the lowest. 

Reply
0 Kudos
LucD
Leadership
Leadership

That probably means that the Where-clause is too restrictive.

How many datastores does this return ?

Get-Datastore | where { ($_.name) -ne "ISOs"}



Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot

foreach  ($VM in $thickVMs) {

          $ds = Get-VMHost -VM $vm | Get-Datastore | where { ($_.name) -ne "ISOs" } |

                   sort FreespaceGB -Descending | select -First 1 | Select -ExpandProperty Name

          $vm | Select Name,@{N="Datastore";E={$ds}}

}

They all show the same datastore, the one with the lowest space right now.  I don't understand why the "where" isn't filtering it the right way.  I guess the order needs to be changed, I'll experiment.

Reply
0 Kudos