Posted in
Virtual Machine Manager,
Windows Powershell,
Windows Server |
1 Comment | 3,349 views | 20/07/2014 09:31
Lets assume that you have a 3 years old storage and you are using it to place your virtual machines on Hyper-V Cluster. Then you bought a new storage box, you created LUNs on it and assign them as a CSV volumes on your existing Hyper-V Cluster. So next achievement would be migrating your virtual machines from old storage to new one.
That migration could be a headache if you have many VMs and many storage LUNs. So in that case, you can use following script to migrate Virtual Machines per CSV Volume. Just you need to specify which CSV volume you would like to drain. So let’s assume that volume is Volume1. I will specify that volume like following:
Start-CSVMigration -TargetVolume "Volume1" |
Start-CSVMigration -TargetVolume "Volume1"
Now you need to specify other CSV Luns as well to filter them. Otherwise, you can migrate a VM to old storage again. So we are filtering them like following:
Start-CSVMigration -TargetVolume "Volume1" -FilteredVolumes "Volume2","Volume3","Volume4" |
Start-CSVMigration -TargetVolume "Volume1" -FilteredVolumes "Volume2","Volume3","Volume4"
So how does it work?
We are creating an array called FilteredVolumes then adding our old storage luns to this array. You will see that I’m also adding $TargetVolume to same array. Because there is a chance to try to migrate a VM to same volume. So that will prevent that kind of issues.
So how can I get destination volume?
$FullQuery = '((Get-ClusterSharedVolume |' + $Query + ' Select -ExpandProperty SharedVolumeInfo | Select @{label="Name";expression={(($_.FriendlyVolumeName).Split("\"))[-1]}},@{label="FreeSpace";expression={($_ | Select -Expand Partition).FreeSpace}} | Sort FreeSpace -Descending)[0]).Name' |
$FullQuery = '((Get-ClusterSharedVolume |' + $Query + ' Select -ExpandProperty SharedVolumeInfo | Select @{label="Name";expression={(($_.FriendlyVolumeName).Split("\"))[-1]}},@{label="FreeSpace";expression={($_ | Select -Expand Partition).FreeSpace}} | Sort FreeSpace -Descending)[0]).Name'
As you can notice from the code below, i’m getting all CSV volumes, filtering old storage volumes, sorting new storage CSVs by their free space and selecting the storage which has biggest available disk space.
So I’ll place VM into that volume:
Move-VMStorage -ComputerName "$ClusterNode" -VMName "$VMName" -DestinationStoragePath "C:\ClusterStorage\$Volume\$VMName" |
Move-VMStorage -ComputerName "$ClusterNode" -VMName "$VMName" -DestinationStoragePath "C:\ClusterStorage\$Volume\$VMName"
You can also allow VMs with passthrough disks and vHBA. You just need to add -AllowPT switch.
Start-CSVMigration -TargetVolume "Volume1" -FilteredVolumes "Volume2","Volume3","Volume4" -AllowPT |
Start-CSVMigration -TargetVolume "Volume1" -FilteredVolumes "Volume2","Volume3","Volume4" -AllowPT
This is what I use to filter VMs with vHBA:
Where {($_ | Select -expand FibreChannelHostBusAdapters) -eq $Null} |
Where {($_ | Select -expand FibreChannelHostBusAdapters) -eq $Null}
If you get the idea, now I can share full script:
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
| function Start-CSVMigration {
<#
.SYNOPSIS
Function to migrate all virtual machines placed on a CSV volume by using Hyper-V Live Storage Migration.
.DESCRIPTION
Lets assume that you have a 3 years old storage and you are using it to place your virtual machines on Hyper-V Cluster.
Then you bought a new storage box, you created LUNs on it and assign them as a CSV volumes on your existing Hyper-V Cluster.
So next achievement would be migrating your virtual machines from old storage to new one.
This script moves virtual machines into different CSV volumes by using Hyper-V Live Storage Migration.
.PARAMETER WhatIf
Display what would happen if you would run the function with given parameters.
.PARAMETER Confirm
Prompts for confirmation for each operation. Allow user to specify Yes/No to all option to stop prompting.
.EXAMPLE
Start-CSVMigration -TargetVolume "Volume1"
.EXAMPLE
Start-CSVMigration -TargetVolume "Volume1" -FilteredVolumes "Volume2","Volume3","Volume4"
.EXAMPLE
Start-CSVMigration -TargetVolume "Volume1" -FilteredVolumes "Volume2","Volume3","Volume4" -AllowPT
.INPUTS
None
.OUTPUTS
None
.NOTES
Author: Yusuf Ozturk
Website: http://www.yusufozturk.info
Email: ysfozy@gmail.com
Date created: 05-July-2014
Last modified: 20-July-2014
Version: 1.4
.LINK
http://www.yusufozturk.info
http://twitter.com/yusufozturk
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param (
# Target Volume
[Parameter(
Mandatory = $true,
HelpMessage = 'The CSV Volume name that you want to drain, like: Volume1')]
$TargetVolume,
# Filtered Volumes
[Parameter(
Mandatory = $false,
HelpMessage = 'The CSV Volume names that you want to filter, like: Volume2')]
[array]$FilteredVolumes,
# Allow Passthrough Disks
[Parameter(
Mandatory = $false,
HelpMessage = 'Allow Passthrough Disks')]
[switch]$AllowPT = $false,
# Debug Mode
[Parameter(
Mandatory = $false,
HelpMessage = 'Debug Mode')]
[switch]$DebugMode = $false
)
# Enable Debug Mode
if ($DebugMode)
{
$DebugPreference = "Continue"
}
else
{
$ErrorActionPreference = "silentlycontinue"
}
# Filtered Volumes
$FilteredVolumes += $TargetVolume
# Clear Query
$Query = $Null;
# Create Query
foreach ($FilteredVolume in $FilteredVolumes)
{
$Query = $Query + ' Where {$_.SharedVolumeInfo.FriendlyVolumeName -notlike "*' + $FilteredVolume + '"} |'
}
# Full Query
$FullQuery = '((Get-ClusterSharedVolume |' + $Query + ' Select -ExpandProperty SharedVolumeInfo | Select @{label="Name";expression={(($_.FriendlyVolumeName).Split("\"))[-1]}},@{label="FreeSpace";expression={($_ | Select -Expand Partition).FreeSpace}} | Sort FreeSpace -Descending)[0]).Name'
# Get Cluster Nodes
$ClusterNodes = Get-Cluster | Get-ClusterNode
foreach ($ClusterNode in $ClusterNodes)
{
# Clear Variables
$VMs = $Null;
# Create VM Array
$VMArray = @()
if ($AllowPT)
{
# Get All Virtual Machines on Target Volume
$VMs = Get-VM -ComputerName "$ClusterNode" | Where ConfigurationLocation -like "C:\ClusterStorage\$TargetVolume\*"
foreach ($VM in $VMs)
{
# Get VM Information
$VMName = $VM.Name
# Add VM to VMArray
$VMArray += $VMName
}
}
else
{
# Get All Virtual Machines on Target Volume
$VMs = Get-VM -ComputerName "$ClusterNode" | Where ConfigurationLocation -like "C:\ClusterStorage\$TargetVolume\*" | Where {($_ | Select -expand FibreChannelHostBusAdapters) -eq $Null}
foreach ($VM in $VMs)
{
# Get VM Information
$VMName = $VM.Name
if ($VM.HardDrives.Path -like "Disk*")
{
# Skipping VM
}
else
{
# Add VM to VMArray
$VMArray += $VMName
}
}
}
foreach ($VMName in $VMArray)
{
Write-Host " "
Write-Host "Working on $VMName .."
Write-Host "Hyper-V Host: $ClusterNode"
# Get Volume Information
$Volume = Invoke-Expression $FullQuery
# Move Virtual Machine
Move-VMStorage -ComputerName "$ClusterNode" -VMName "$VMName" -DestinationStoragePath "C:\ClusterStorage\$Volume\$VMName"
Write-Host "Done."
}
}
} |
function Start-CSVMigration {
<#
.SYNOPSIS
Function to migrate all virtual machines placed on a CSV volume by using Hyper-V Live Storage Migration.
.DESCRIPTION
Lets assume that you have a 3 years old storage and you are using it to place your virtual machines on Hyper-V Cluster.
Then you bought a new storage box, you created LUNs on it and assign them as a CSV volumes on your existing Hyper-V Cluster.
So next achievement would be migrating your virtual machines from old storage to new one.
This script moves virtual machines into different CSV volumes by using Hyper-V Live Storage Migration.
.PARAMETER WhatIf
Display what would happen if you would run the function with given parameters.
.PARAMETER Confirm
Prompts for confirmation for each operation. Allow user to specify Yes/No to all option to stop prompting.
.EXAMPLE
Start-CSVMigration -TargetVolume "Volume1"
.EXAMPLE
Start-CSVMigration -TargetVolume "Volume1" -FilteredVolumes "Volume2","Volume3","Volume4"
.EXAMPLE
Start-CSVMigration -TargetVolume "Volume1" -FilteredVolumes "Volume2","Volume3","Volume4" -AllowPT
.INPUTS
None
.OUTPUTS
None
.NOTES
Author: Yusuf Ozturk
Website: http://www.yusufozturk.info
Email: ysfozy@gmail.com
Date created: 05-July-2014
Last modified: 20-July-2014
Version: 1.4
.LINK
http://www.yusufozturk.info
http://twitter.com/yusufozturk
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param (
# Target Volume
[Parameter(
Mandatory = $true,
HelpMessage = 'The CSV Volume name that you want to drain, like: Volume1')]
$TargetVolume,
# Filtered Volumes
[Parameter(
Mandatory = $false,
HelpMessage = 'The CSV Volume names that you want to filter, like: Volume2')]
[array]$FilteredVolumes,
# Allow Passthrough Disks
[Parameter(
Mandatory = $false,
HelpMessage = 'Allow Passthrough Disks')]
[switch]$AllowPT = $false,
# Debug Mode
[Parameter(
Mandatory = $false,
HelpMessage = 'Debug Mode')]
[switch]$DebugMode = $false
)
# Enable Debug Mode
if ($DebugMode)
{
$DebugPreference = "Continue"
}
else
{
$ErrorActionPreference = "silentlycontinue"
}
# Filtered Volumes
$FilteredVolumes += $TargetVolume
# Clear Query
$Query = $Null;
# Create Query
foreach ($FilteredVolume in $FilteredVolumes)
{
$Query = $Query + ' Where {$_.SharedVolumeInfo.FriendlyVolumeName -notlike "*' + $FilteredVolume + '"} |'
}
# Full Query
$FullQuery = '((Get-ClusterSharedVolume |' + $Query + ' Select -ExpandProperty SharedVolumeInfo | Select @{label="Name";expression={(($_.FriendlyVolumeName).Split("\"))[-1]}},@{label="FreeSpace";expression={($_ | Select -Expand Partition).FreeSpace}} | Sort FreeSpace -Descending)[0]).Name'
# Get Cluster Nodes
$ClusterNodes = Get-Cluster | Get-ClusterNode
foreach ($ClusterNode in $ClusterNodes)
{
# Clear Variables
$VMs = $Null;
# Create VM Array
$VMArray = @()
if ($AllowPT)
{
# Get All Virtual Machines on Target Volume
$VMs = Get-VM -ComputerName "$ClusterNode" | Where ConfigurationLocation -like "C:\ClusterStorage\$TargetVolume\*"
foreach ($VM in $VMs)
{
# Get VM Information
$VMName = $VM.Name
# Add VM to VMArray
$VMArray += $VMName
}
}
else
{
# Get All Virtual Machines on Target Volume
$VMs = Get-VM -ComputerName "$ClusterNode" | Where ConfigurationLocation -like "C:\ClusterStorage\$TargetVolume\*" | Where {($_ | Select -expand FibreChannelHostBusAdapters) -eq $Null}
foreach ($VM in $VMs)
{
# Get VM Information
$VMName = $VM.Name
if ($VM.HardDrives.Path -like "Disk*")
{
# Skipping VM
}
else
{
# Add VM to VMArray
$VMArray += $VMName
}
}
}
foreach ($VMName in $VMArray)
{
Write-Host " "
Write-Host "Working on $VMName .."
Write-Host "Hyper-V Host: $ClusterNode"
# Get Volume Information
$Volume = Invoke-Expression $FullQuery
# Move Virtual Machine
Move-VMStorage -ComputerName "$ClusterNode" -VMName "$VMName" -DestinationStoragePath "C:\ClusterStorage\$Volume\$VMName"
Write-Host "Done."
}
}
}
It’s enough to run this script on one of the Cluster Node, so that will start migrating virtual machines. That will also give an output, so you will be able to see which VM you are migrating.
This is also an example how you can automate storage migrations:
1
2
3
4
5
6
7
8
9
10
11
| $Volumes = @()
$Volumes += "Volume1"
$Volumes += "Volume2"
$Volumes += "Volume3"
$Volumes += "Volume4"
$Volumes += "Volume5"
foreach ($Volume in $Volumes)
{
Start-CSVMigration -TargetVolume "$Volume" -FilteredVolumes "Volume1","Volume2","Volume3","Volume4","Volume5","Volume6","Volume7","Volume8","Volume9","Volume10","Volume11","Volume12","Volume13","Volume14","Volume15"
} |
$Volumes = @()
$Volumes += "Volume1"
$Volumes += "Volume2"
$Volumes += "Volume3"
$Volumes += "Volume4"
$Volumes += "Volume5"
foreach ($Volume in $Volumes)
{
Start-CSVMigration -TargetVolume "$Volume" -FilteredVolumes "Volume1","Volume2","Volume3","Volume4","Volume5","Volume6","Volume7","Volume8","Volume9","Volume10","Volume11","Volume12","Volume13","Volume14","Volume15"
}
I hope you will find it useful. See you!