VMware Cloud Community
Bgaru
Contributor
Contributor
Jump to solution

storage vmotion thin datastore

Looking for powercli to kick storage vmotion when the datastore thin provision reach 120% migrate vms to datastores which has less then 40% provision space.

condition only storage vmotion vm name starting with W78*, and vms used disk less then 1TB and should have no Physical rdms attached to vms then only it should like

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I forgot some commas at the end of the calculated properties.

The code above is updated.

Can you also give this updated version a try?

Get-VM -Name (Import-Csv -Path .\vmnames.csv -UseCulture).vmname |

ForEach-Object -Process {

    $badDS = $ds |

        where{$_.ExtensionData.Summary.Capacity -ne 0 -and

            ($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Uncommitted)/$_.ExtensionData.Summary.Capacity -gt 1.2} |

        select -ExpandProperty Name

    if($badDS -contains (Get-Datastore -RelatedObject $_).Name){

        $targetDS = Get-Datastore |

            where{$badDS -notcontains $_.Name -and

                $_.ExtensionData.Summary.Capacity -ne 0 -and

            ($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Uncommitted)/$_.ExtensionData.Summary.Capacity -lt 0.4} |

            Sort-Object -Descending -Property {($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Uncommitted)/$_.ExtensionData.Summary.Capacity} |

            select -First 1

        if($targetDS){

            Move-VM -VM $_ -Datastore $targetDS -Confirm:$false

        }

        else{

            Write-Host "No target DS found for $($_.Name)"

        }

    }

}


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

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Are you looking for a script that you will run ad-hoc?
What do you already have?

And where does it go wrong?


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

Reply
0 Kudos
Bgaru
Contributor
Contributor
Jump to solution

HI Lucd,

Thanks for looking into my post.

yes i am looking script to run on ad-hoc and further i will integrate with some task scheduled weekly once.

i started with below. exploring with conditions

Kick storage vmotion when the datastore thin provision reach 120% migrate vms to datastore which has less then 40% provision space.

storage vmotion vm name starting with W78*, and vm provisioned disk less then 1TBGB no Physical rdms attached to vms.

$VMsToRelocate = Import-Csv ".\ListOfVMsToRelocate.csv"

$Datastore = Get-Datastore -Name "SouthZone_001_Lun20"

foreach ($VM in $VMsToRelocate)

{

Write-Host "Relocating VM:" $VM.Name "to" $Datastore

Get-VM -Name $VM.Name | Move-VM -datastore $Datastore > $null

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could try something like this.

It assumes you have a CSV with a column 'vmname'.

For $targetDS the script uses all possible datastores.

This can be changed by adapting that line with an explicit name or by adding a where-clause.

Get-VM -Name (Import-Csv -Path .\vmnames.csv -UseCulture).vmname |

ForEach-Object -Process {

    $badDS = $ds |

        where{($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/$_.ExtensionData.Summary.Capacity -gt 1.2} |

        select -ExpandProperty Name

    if($badDS -contains (Get-Datastore -RelatedObject $_).Name){

        $targetDS = Get-Datastore |

            where{$badDS -notcontains $_.Name -and

                ($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/$_.ExtensionData.Summary.Capacity -lt 0.4} |

            Sort-Object -Descending -Property {($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/$_.ExtensionData.Summary.Capacity} |

            select -First 1

        if($targetDS){

            Move-VM -VM $_ -Datastore $targetDS -Confirm:$false

        }

        else{

            Write-Host "No target DS found for $($_.Name)"

        }

    }

}


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

Reply
0 Kudos
Bgaru
Contributor
Contributor
Jump to solution

Thanks for your time and help me.

executed getting below error

Attempted to divide by zero.

At line:6 char:15

+ ...       where{($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Sum ...

+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], RuntimeException

    + FullyQualifiedErrorId : RuntimeException

in the given code do we need to set $ds variable with value right, What is the $ds value

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like you might have some datastores with a capacity of 0.

Run the following to check

Get-Datastore | Select Name,

    @{N='FreeSpace';E={$_.ExtensionData.Summary.FreeSpace}},

    @{N='Uncommitted';E={$_.ExtensionData.Summary.Uncommitted}},

    @{N='Capacity';E={$_.ExtensionData.Summary.Capacity}}


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

Reply
0 Kudos
Bgaru
Contributor
Contributor
Jump to solution

ran the given script, i am able to fetch only freespace details only

   @{N='Uncommitted';E={$_.ExtensionData.Summary.Uncommitted}}

    @{N='Capacity';E={$_.ExtensionData.Summary.Capacity}}

output as below

N                                                        

E                                                        

N                                                        

E                                                        

Reply
0 Kudos
Bgaru
Contributor
Contributor
Jump to solution

i tried below to findout capacity with 0, non of the datastore capacity with 0. All datastore has with desired capicity value

Get-Datastore | Select-Object Name,

    @{N = ”FreespaceGB”; E = { “{0:n2}” -f ($_.FreespaceGB) } },

    CapacityGB,

    @{N = ”Provisioned”; E = { “{0:n2}” -f ($_.CapacityGB – $_.FreespaceGB + ($_.extensiondata.summary.uncommitted / 1GB)) } },

    @{N = 'Used Space(%)'; E = { [math]::Round((($_.CapacityGB - $_.FreeSpaceGB) / $_.CapacityGB * 100), 1) } } | select name, CapacityGB

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I forgot some commas at the end of the calculated properties.

The code above is updated.

Can you also give this updated version a try?

Get-VM -Name (Import-Csv -Path .\vmnames.csv -UseCulture).vmname |

ForEach-Object -Process {

    $badDS = $ds |

        where{$_.ExtensionData.Summary.Capacity -ne 0 -and

            ($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Uncommitted)/$_.ExtensionData.Summary.Capacity -gt 1.2} |

        select -ExpandProperty Name

    if($badDS -contains (Get-Datastore -RelatedObject $_).Name){

        $targetDS = Get-Datastore |

            where{$badDS -notcontains $_.Name -and

                $_.ExtensionData.Summary.Capacity -ne 0 -and

            ($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Uncommitted)/$_.ExtensionData.Summary.Capacity -lt 0.4} |

            Sort-Object -Descending -Property {($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Uncommitted)/$_.ExtensionData.Summary.Capacity} |

            select -First 1

        if($targetDS){

            Move-VM -VM $_ -Datastore $targetDS -Confirm:$false

        }

        else{

            Write-Host "No target DS found for $($_.Name)"

        }

    }

}


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

Reply
0 Kudos