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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<#
    .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 *