VMware Datastore Maintenance Mode PowerCLI Script


If you have Enterprise Plus licensing then you have Storage DRS available to you which offers a Datastore Maintenance Mode feature. When I tested this feature I did not like the fact that it kicked off a migration for all VMs on that datastore at the same time. This script if for someone that doesn’t have Enterprise Plus licensing or if you do have Enterprise Plus you would prefer migrating one VM at a time.

<#
    .SYNOPSIS
    Storage Migrate VMs off of datastore(s)
    
#>

$min_freespace = 50 # Minimum desired freespace on datastores in GB

$vCenter = "vcenter"

Write-Host ""
Write-Host "Logging into $vCenter"
Connect-VIServer $vCenter | out-null

while (!$ds_source) { # Keep promting for a datastore until a valid one is entered
    do{
        try{
            $invaled = $false
            Write-Host ""
            $ds_source_input = Read-Host "Datastore name to move VMs off of"
        }
        catch {$invalid = $true}
    }
    until ($ds_source_input -ne '')

    $ds_source = @(Get-Datastore $ds_source_input)
    if (!$ds_source) {
        Write-Host ""
        Write-Host "Error:  Datastore $ds_source_input not found" -foregroundcolor "red"
        Write-Host ""
        Continue
    }
}

# Check if there are any templates on the datastore
foreach ($ds in $ds_source) {
    $templates = Get-Template -Datastore $ds.Name
    if ($templates) {
        Write-Host ""
        Write-Host "Warning:  Templates cannot be moved.  Convert them to VMs first." -foregroundcolor "yellow"
        Write-Host ""
        Write-Host "$($ds.Name) has the following templates:" -foregroundcolor "yellow"
        Write-host ""

        foreach ($VM in $templates) {
            Write-Host "`t$($VM.Name)" -foregroundcolor "yellow"
        }
    }
}

# If we're only working with one datastore ask how to migrate
if ($ds_source.Count -eq 1) {
    do{
        try{
            $invaled = $false
            Write-Host ""
            Write-Host "    (1) Specify destination datastore"
            Write-Host "    (2) Automatic best fit"
            Write-Host ""
            $method = Read-Host "Select which method to use"
        }
        catch {$invalid = $true}
    }
    until ($method -eq '1' -or $method -eq '2')

    if ($method -eq 1) {$method = "specific"}
    if ($method -eq 2) {$method = "auto"}
}
# Otherwise we only use the auto method
else {
    $method = "auto"
}

if ($method -eq "specific") {
    while (!$ds_dest) { # Keep promting for a datastore until a valid one is entered
        do{
            try{
                $invaled = $false
                Write-Host ""
                $ds_dest_input = Read-Host "Destination datastore name"
            }
            catch {$invalid = $true}
        }
        until ($ds_dest_input -ne '')  

        $ds_dest = Get-Datastore $ds_dest_input
        if (!$ds_dest) {
            Write-Host ""
            Write-Host "Error:  Datastore $ds_dest_input not found" -foregroundcolor "red"
            Write-Host ""
            Continue
        }
        elseif ($ds_dest.Count -gt 1) {
            $ds_dest = "" # reset destination datastore in order to prompt again
            Write-Host ""
            Write-Host "Error:  More than one datastore matches $ds_dest_input" -foregroundcolor "red"
            Write-Host ""
            Continue
        }
        # Check if the destination datastore has the capacity to move all VMs from the source datastore
        elseif (($ds_source[0].CapacityGB - $ds_source[0].FreeSpaceGB) -gt ($ds_dest.FreeSpaceGB - $min_freespace) ) {
            Write-Host ""
            Write-Host "Error:  Destination datastore $($ds_dest.Name) does not have enough freespace" -foregroundcolor "red"
            Write-Host ""
            Continue
        }
    }    
}


if ($ds_source) {
    Write-Host ""
    Write-Host "You've selected to storage vMotion all VMs from the folling datastore(s)"
    Write-Host ""    
    foreach ($ds in $ds_source) {
        Write-Host "`t$($ds.Name)"
    }
}

do{
    try{
        $invaled = $false
        Write-Host ""
        $confirm = Read-Host "Do you want to continue (y/n)"
    }
    catch {$invalid = $true}
}
until (
    $confirm -eq 'y' -or $confirm -eq 'yes' -or`
    $confirm -eq 'n' -or $confirm -eq 'no'
)
if ($confirm -eq 'n' -or $confirm -eq 'no') {
    Disconnect-VIServer * -Confirm:$false
    Write-Host ""
    exit
}


Write-Host ""
Write-Host "Preparing to move the following VMs"
Write-Host ""
    
foreach ($ds in $ds_source) {
    # Get a list of all the VMs on the source datastore    
    $VMs_to_move = @(Get-VM -Datastore $ds | Sort-Object ProvisionedSpaceGB -descending)

    foreach ($VM in $VMs_to_move) {
        Write-Host "`t$($VM.Name)"
    }
}

Write-Host ""

if ($method -eq "specific") {
    foreach ($ds in $ds_source) {
        # Get a list of all the VMs on the source datastore    
        $VMs_to_move = @(Get-VM -Datastore $ds | Sort-Object ProvisionedSpaceGB -descending)

        foreach ($VM in $VMs_to_move) {
            Write-Host "Migrating $($VM.Name) to $ds_dest..."
            move-vm -VM $VM -Datastore $ds_dest | out-null
        }
    }
}

if ($method -eq "auto") {
    foreach ($ds in $ds_source) {
        # Get a list of all the VMs on the source datastore    
        $VMs_to_move = @(Get-VM -Datastore $ds | Sort-Object ProvisionedSpaceGB -descending)

        foreach ($VM in $VMs_to_move) {
            # Find an available datastore only from the host where the VM is currently located
            # Require it to be VMS - remvove the "...VMFS..." line if you want to include NAS
            # Require the storage to be shared - remove the "...Host.Count..." line for standalone hosts
            $ds_dest = @(get-VMHost $VM.VMHost | get-datastore | `
                where {$_.Type -eq "VMFS" -and `
                       $_.ExtensionData.Host.Count -gt 1 -and `
                       $_.FreeSpaceGB -gt ($min_freespace + $VM.ProvisionedSpaceGB)
                      } | Sort-Object FreeSpaceGB -descending)
                      
            # Remove all the source datastores as options for the destination datastore
            foreach ($ds_s in $ds_source) {
                $ds_dest = @($ds_dest | where {$_.Name -ne $ds_s.Name})
            }
            
            if ($ds_dest) {
                Write-Host "Migrating $($VM.Name) to $($ds_dest[0])..."
                move-vm -VM $VM -Datastore $ds_dest[0] | out-null
            }
            else {
                Write-Host ""
                Write-Host "Error:  Unable to find a datastore with capacity for $($VM.Name)" -foregroundcolor "red"
                Write-Host ""
            }
        }
    }
}

Disconnect-VIServer * -Confirm:$false
Write-Host ""

Leave a comment

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