-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlign-VMsWithStorage.ps1
284 lines (205 loc) · 9.66 KB
/
Align-VMsWithStorage.ps1
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<#
.DESCRIPTION
Align-VMsWithStorage is a script that can be used to align your VMs
with your Cluster Shared Volumes. With Storage Spaces Direct all writes
are routed through the owner of the CSV which can cause ineffiecient IO paths.
This script makes sure your VM is running on the same host which owns the CSV
the VM is running on.
The script finds the volume where most of your disks of your VMs are on and moves the VM there.
If you have equally spread disks, it will use the first disk as target.
.INPUTS
None
.OUTPUTS
None
.NOTES
Author: Darryl van der Peijl
Website: http://www.DarrylvanderPeijl.nl/
Email: DarrylvanderPeijl@outlook.com
Date created: 31.december.2018
Last modified: 17.january.2019
Version: 0.9.3
.LINK
http://www.DarrylvanderPeijl.nl/
https://twitter.com/DarrylvdPeijl
#>
[CmdletBinding(SupportsShouldProcess = $true,ConfirmImpact='High')]
param (
[alias("CimSession ")]
[Parameter(Position = 0)][String]$Clustername = "localhost",
[switch]$QuickMigration,
[int]$NodePhysicalmemorybufferGB = 8
)
begin{
#region:Helper Functions
Function GetCSVIDbyPath {
param ($CSVpath)
Begin {
Write-Verbose -Message "Trying to find CSV ID with path $CSVpath"
}
Process {
$CSVhash.keys | ForEach-Object {
#If value found, write out key
If ($CSVhash[$_] -like $CSVpath) {
Write-Verbose -Message "Found ID for $CSVPath - $_"
Write-output $_
}
}
}
}
#endregion
#region GatherInfoNeeded
#Cluster
$Cluster = Get-Cluster -Name $Clustername
$Clustername = $Cluster.Name + "." + $cluster.Domain
if (!($Cluster)) {
Write-Error "Could not find Cluster with name $Clustername"
}
Else {
$domain = $Cluster.Domain
Write-Verbose "Found Cluster $Clustername in domain $domain"
}
#ClusterNodes
$clusternodes = Get-ClusterNode -cluster $Clustername
if (!($clusternodes)) {
Write-Error "Could not find cluster nodes in cluster $Clustername"
}
Else {
$count = ($clusternodes).count
Write-Verbose "Found $count cluster nodes in cluster $Clustername"
}
#ClusterSharedVolumes
Write-Verbose "Getting CSV(s) from $Clustername"
$Clustersharedvolumes = Get-ClusterSharedVolume -Cluster $Clustername
if (!($Clustersharedvolumes)) {
Write-Error "Could not find Cluster Shared Volumes on $Clustername"
}
Else {
$count = ($Clustersharedvolumes).count
Write-Verbose "Found $count CSV(s) on cluster $Clustername"
}
#endregion
}
process{
#region interpret information
$CSVhash = @{}
Foreach ($Clustersharedvolume in $Clustersharedvolumes) {
$Matches = $null
#Extract the CSV volume names from cluster resource name
$Null = $Clustersharedvolume.name -match ".*?\((.*?)\)"
if ($Matches) {
$CSVname = $($Matches[1])
Write-Verbose -Message "Regex matched and found $CSVname"
}else {
Write-Verbose -Message "Regex did not match, probably renamed CSV"
$CSVname = $Clustersharedvolume.name
$FirstClusternode = $clusternodes[0].Name
Write-Verbose -Message "Checking if virtualdisk with name $CSVname exist on $FirstClusternode"
If ((Get-VirtualDisk $CSVname -CimSession $FirstClusternode))
{
Write-Verbose -Message "virtualdisk with name $CSVname exist on $FirstClusternode"
}else{
Throw "Cannot find CSV with name $CSVname, something is wrong. Exitting."
Exit
}
}
Write-Verbose -Message "Gathering information of CSV $CSVname"
$CSVhash[($Clustersharedvolume.Id).ToString()] = @($CSVname, $Clustersharedvolume.OwnerNode.Name, $Clustersharedvolume.SharedVolumeInfo.FriendlyVolumeName)
}
## Find VMs and the CSV they live on and the host they live on
## exclude VMs with disks on multiple CSVs (for now?)
$VMhash = @{}
$VMs = $null
Foreach ($clusternode in $clusternodes) {
[array]$VMs += Get-VM -ComputerName $clusternode
}
Foreach ($VM in $VMs) {
Write-Verbose -Message "Finding disks for $($VM.name)"
$CSVtemparray = @()
Foreach ($disk in ($vm | Get-VMHardDiskDrive)) {
$diskpathsplit = $disk.path -split '\\'
$CSVpath = $diskpathsplit[0] + "\" + $diskpathsplit[1] + "\" + $diskpathsplit[2]
Write-Verbose -Message "Found disk on $CSVpath"
Write-Verbose -Message "Trying to find CSV ID through GetCSVIDbyPath function"
$TempCSVID = GetCSVIDbyPath -CSVpath $CSVpath
$CSVtemparray += $TempCSVID
}
$CSVID = ($CSVtemparray | Group-Object | Sort-Object Count -descending | Select-Object -First 1).name
Write-Verbose -Message "Adding $($VM.Name) to hashtable using CSV ID $CSVID"
$vmid = $VM.VMID.ToString()
$VMhash[$vmid] = @($VM.name, $CSVID, $VM.ComputerName, $VM.MemoryAssigned, $VM.State)
}
##Create task list of volumes to move to be in optimal condition
$VMstoMove = @{}
#Check if VM is on same disk as node
$VMhash.Keys | ForEach-Object {
$VMOwner = $VMhash[$_][2]
$CSVOwner = $CSVhash[$VMhash[$_][1]][1]
if ($VMOwner -eq $CSVOwner) {
Write-Verbose -Message "VM with ID $_ is on same host as CSV"
}
else {
Write-Verbose -Message "VM with ID $_ is on different host as CSV and can be optimized"
$vmid = $_.ToString()
$VMstoMove[$vmid] = @($VMhash[$_][0], $CSVOwner, $VMhash[$_][3], $VMhash[$_][4])
}
}
#endregion
#region Move VMs
Write-Output "Found $($($VMstoMove).count) VM(s) to be optimized."
Write-Verbose "Quickmigration switch = $Quickmigration"
$VMstoMove.Keys | ForEach-Object {
$vmid = $_.ToString()
$vmname = $VMstoMove[$_][0]
$targetnode = $VMstoMove[$_][1]
$VMstate = $VMstoMove[$_][3]
$VMmem = $VMstoMove[$_][2] / 1024 / 1024 / 1024
Write-Verbose -Message "Intent to move $vmname to $targetnode"
$NodeFreeMem = ((Get-WMIObject Win32_OperatingSystem -computername $targetnode).FreePhysicalMemory / 1024 / 1024)
Write-Verbose -Message "$targetnode has $NodeFreeMem of free physical memory,$VMname needs $VMmem"
If (($NodeFreeMem + $NodePhysicalmemorybufferGB) -gt $VMmem) {
Write-Verbose -Message "$targetnode has enough resources to host $VMname"
if ($VMstate -eq "Off" -or $Quickmigration -eq $true) {
if ($PSCmdlet.ShouldProcess(
("{0}" -f $vmname),
("Migrating to {0} using quick migration" -f $targetnode)
)
) {
Write-Output "VM $vmname is $VMstate and being moved to $targetnode using quick migration"
$MoveAction = Move-ClusterVirtualMachineRole -Cluster $Clustername -VMId $vmid -Node $targetnode -MigrationType Quick
If ($MoveAction.OwnerNode -eq $targetnode) {
Write-Output "VM $vmname succesfully moved to $targetnode"
}
Else {
Throw "VM $vmname not succesfully moved to $targetnode,exiting!"
Exit
}
}
}
ElseIf ($VMstate -eq "Running") {
if ($PSCmdlet.ShouldProcess(
("{0}" -f $vmname),
("Migrating to {0} using live migration" -f $targetnode)
)
) {
Write-Output "VM $vmname is running and being moved to $targetnode using live migration"
$MoveAction = Move-ClusterVirtualMachineRole -Cluster $Clustername -VMId $vmid -Node $targetnode
If (($MoveAction.OwnerNode -eq $targetnode) -and ($MoveAction.State -eq "Online")) {
Write-Output "VM $vmname succesfully moved to $targetnode"
}
Else {
Throw "VM $vmname not succesfully moved to $targetnode,exiting!"
Exit
}
}
}
Else {
Write-Verbose "Status of VM $vmname is $VMstate"
Write-Output "Status of VM $vmname is other then Running or Off, skipping VM."
}
}
Else {
Write-Verbose -Message "Not enough resources for $vmname on $targetnode"
}
}
#endregion
}