Hi,
i'm trying to write a function to take as input a list of VM's plus, a Datastore name.
The function first needs to verify that there's enough freespace in the Datastore and that the VM is powered down.
Once this is complete, the VM should be moved to the new Datastore and, it's disks converted from Thick to Thin format.
I've attempted the below but, it's not throwing any error and, is not doing anything either, i.e. not powering down the VM or performing the svMotion
Function ConvertThickToThin
{
[cmdletBinding()]
param (
[Parameter (Position=0,mandatory=$true)]
[String[]]$vm = @(),
[Parameter (Position=1,mandatory=$true)]
[String]$DestinationDatastore
)
Get-VM -Name $vm | where {(Get-HardDisk -VM $_).DiskType -contains 'Thick'} | foreach {
if ($DestinationDatastore.freespacegb -ge ($vm.provisionedspaceGB*1.20)) {
if ($vm.powerstate -match 'PoweredOn') {
Shutdown-VMGuest $v -Confirm:$false -ErrorAction SilentlyContinue
}
Move-VM -VM.name -Datastore $DestinationDatastore -DiskStorageFormat Thin
}
}
}
Also, can someone tell me the difference between
Thanks
Since you are doing a svMotion, the VM doesn't need to be powered down.
And the svMotion will do the conversion.
You might have missed my 2GB explanation in my previous answer?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I'm afraid you are testing the wrong property, it should be StorageFormat.
Further, you pass the datastore as a string, which doesn't have the FreeSpaceGB.
First get the Datastore object.
The 2GB indication defines a harddisk with extents of maximum 2GB.
The reason for doing that is that these can be used by other VMware products (think Workstation), and they can be easier burned to DVD.
Try like this
Function ConvertThickToThin
{
[cmdletBinding()]
param (
[Parameter (Position=0,mandatory=$true)]
[String[]]$vm = @(),
[Parameter (Position=1,mandatory=$true)]
[String]$DestinationDatastore
)
$tgtDS = Get-Datastore -Name $DestinationDatastore
Get-VM -Name $vm | where {(Get-HardDisk -VM $_).StorageFormat -contains 'Thick'} | foreach {
if (($tgtDS.freespacegb -ge ($vm.provisionedspaceGB*1.20)) {
if ($vm.powerstate -match 'PoweredOn') {
Shutdown-VMGuest $v -Confirm:$false -ErrorAction SilentlyContinue
}
Move-VM -VM.name -Datastore $tgtDS -DiskStorageFormat Thin
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc,
There were a few typo's but, the following is working almost 100%
What's not working is the VM powerdown. The VM's don't powerdown but, the disk format is still converted, is the powerdown necessary in ESXi 6.0 ?
Function ConvertThickToThin
{
<#
.SYNOPSIS
Convert VM Disk Format from Thick to Thin
.DESCRIPTION
Accept a list of VM's and a destination datastore
Verify that the VM contains Thick Disks
Verify 20% free space exists on the destination Datastore and if yes, power down the VM
svMotion the VM to the destination datastore, converting it's Thick Disks to Thin in the process
.EXAMPLE
ConvertThickToThin -vm vm1name -destinationdatastore ds_name
.EXAMPLE
ConvertThickToThin -vm vm1name, vm2name,vm3name -destinationdatastore ds_name
#>
[cmdletBinding()]
param (
[Parameter (Position=0,mandatory=$true)]
[String[]]$vm = @(),
[Parameter (mandatory=$true)]
[String]$DestinationDatastore
)
$tgtDS = Get-Datastore -Name $DestinationDatastore
Get-VM -Name $vm | Where-Object {(Get-HardDisk -VM $_).StorageFormat -contains 'Thick'} | ForEach-Object {
if (($tgtDS.freespacegb) -ge ($vm.provisionedspaceGB*1.20)) {
if ($vm.powerstate -match 'PoweredOn') {
Write-Host "VM $vm will be shut down to perform a THICK to Thin conversion " -ForegroundColor Red
Shutdown-VMGuest $vm -Confirm:$false # -ErrorAction SilentlyContinue
}
Move-VM -VM $vm -Datastore $tgtDS -DiskStorageFormat Thin
}
}
}
Second question: what's the difference between a Thin and Thin2GB Disk ?
I haven't been able to find much/anything on the internet
thanks,
Jason
Since you are doing a svMotion, the VM doesn't need to be powered down.
And the svMotion will do the conversion.
You might have missed my 2GB explanation in my previous answer?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc, I did miss your 2gb explanation in your last comment.
as a powerdown isn't needed with svMotion I'll remove that freom the code
thank you again,
Jason