Posted in
Virtual Machine Manager,
Windows Powershell |
No Comment | 4,452 views | 15/02/2014 12:41
You can use this script to move your VMs into another Cloud (Cloud Migration) on SCVMM 2012 R2.
This script only works on SCVMM 2012 R2 due to vHBA control.
You can remove vHBA control to make it work on SCVMM 2012 SP1.
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
| # Parameters
$DestinationCloud = "New Cloud"
# Create VM Array
$VMArray = New-Object System.Collections.ArrayList
$VMArray.Clear();
# Add VMs into Array
$AddArray = $VMArray.Add("VM01")
$AddArray = $VMArray.Add("VM02")
$AddArray = $VMArray.Add("VM03")
$AddArray = $VMArray.Add("VM04")
# Get Destination Cloud
$Cloud = Get-SCCloud -Name $DestinationCloud
# Cloud Host Group Path
$HostGroupPath = (($Cloud.HostGroup)[0]).Path + "*"
foreach ($VMName in $VMArray)
{
# Get VM
$VM = Get-SCVirtualMachine -Name $VMName
# Output
Write-Host VM Name: $VMName
Write-Host " "
if ($VM.HasPassthroughDisk -eq $False -and $VM.HasVirtualFibreChannelAdapters -eq $False)
{
# Current Cloud
$CurrentCloud = Get-SCCloud -Name $VM.Cloud.Name
# Current Hyper-V Host
$CurrentVMHost = Get-SCVMHost -ComputerName $VM.VMHost.Name
# Create Job Guid
$JobGuid = [System.Guid]::NewGuid().toString()
# Remove from Cloud
$SetCloud = $VM | Set-SCVirtualMachine -RemoveFromCloud
# Get Best Available Hyper-V Host
$VMHostName = ((Get-SCVMHost | Where {$_.VMHostGroup -like $HostGroupPath -and $_.CoresPerCPU -eq $CurrentVMHost.CoresPerCPU -and $_.CPUArchitecture -eq $CurrentVMHost.CPUArchitecture -and $_.CPUFamily -eq $CurrentVMHost.CPUFamily } | Select Name,AvailableMemory | Sort AvailableMemory -Descending)[0]).Name
# Output
Write-Host Target Host: $VMHostName
# Get Best Available CSV
$VolumeName = ((Get-SCStorageVolume | Where {$_.VMHost -eq $VMHostName -and $_.IsClusterSharedVolume -eq $True} | Select Name,FreeSpace | Sort FreeSpace -Descending)[0]).Name
# Output
Write-Host Target Volume: $VolumeName
Write-Host " "
# Get Hyper-V Host Information
$VMHost = Get-SCVMHost -ComputerName $VMHostName
[int64]$VMHostAvailableMemory = [int64]$VMHost.AvailableMemory + 10240 # Leave 10 GB Available Memory
# Get CSV Information
$Volume = Get-SCStorageVolume -Name $VolumeName -VMHost $VMHostName
[int64]$VolumeFreeSpace = [int64]$Volume.FreeSpace + 107374182400 # Leave 100 GB Free Space
# Control Free Memory
if ($VM.Memory -lt $VMHostAvailableMemory -and $VM.TotalSize -lt $VolumeFreeSpace)
{
# Get Virtual Network Adapters
$VirtualNetworkAdapters = $VM | Get-SCVirtualNetworkAdapter
foreach ($VirtualNetworkAdapter in $VirtualNetworkAdapters)
{
# Clear VM Network
$VMNetwork = $Null;
# Get VM Network
$VMNetwork = Get-SCVMNetwork | Where {$_.Name -eq $VirtualNetworkAdapter.VMNetwork.Name}
if (!$VMNetwork)
{
$VMNetwork = Get-SCVMNetwork | Where {$_.VMSubnet.SubnetVLANs.VLanID -eq $VirtualNetworkAdapter.VLanID}
}
# Destination Virtual Network
$VirtualNetwork = ((Get-VM -Cloud $Cloud | Where {$_.VirtualNetworkAdapters.VMNetwork.Name -eq $VirtualNetworkAdapter.VMNetwork.Name -and $_.VirtualNetworkAdapters.VirtualNetwork})[0]).VirtualNetworkAdapters.VirtualNetwork
# Set VM Network Adapter
$SetSCVirtualNetworkAdapter = Set-SCVirtualNetworkAdapter -VirtualNetworkAdapter $VirtualNetworkAdapter -VirtualNetwork $VirtualNetwork -VMNetwork $VMNetwork -JobGroup $JobGuid
}
# Move VM
$MoveSCVirtualMachine = $VM | Move-SCVirtualMachine -VMHost $VMHostName -HighlyAvailable $True -UseLAN -UseDiffDiskOptimization -JobGroup $JobGuid -Path $VolumeName
Write-Host "Migration process is finished."
Write-Host "Please check job results to ensure that if operation is successful.."
Write-Host " "
Write-Host " "
# Set Cloud
$SetCloud = $VM | Set-SCVirtualMachine -Cloud $Cloud
# Refresh VM
$RefreshVM = $VM | Refresh-VM
}
else
{
Write-Host "Not enough resources to move VM.."
Write-Host "Skipping migration.."
Write-Host " "
Write-Host " "
# Set Cloud
$SetCloud = $VM | Set-SCVirtualMachine -Cloud $CurrentCloud
}
}
else
{
Write-Host "VM has Pass-through disks or vHBA.."
Write-Host "Skipping migration.."
Write-Host " "
Write-Host " "
}
} |
# Parameters
$DestinationCloud = "New Cloud"
# Create VM Array
$VMArray = New-Object System.Collections.ArrayList
$VMArray.Clear();
# Add VMs into Array
$AddArray = $VMArray.Add("VM01")
$AddArray = $VMArray.Add("VM02")
$AddArray = $VMArray.Add("VM03")
$AddArray = $VMArray.Add("VM04")
# Get Destination Cloud
$Cloud = Get-SCCloud -Name $DestinationCloud
# Cloud Host Group Path
$HostGroupPath = (($Cloud.HostGroup)[0]).Path + "*"
foreach ($VMName in $VMArray)
{
# Get VM
$VM = Get-SCVirtualMachine -Name $VMName
# Output
Write-Host VM Name: $VMName
Write-Host " "
if ($VM.HasPassthroughDisk -eq $False -and $VM.HasVirtualFibreChannelAdapters -eq $False)
{
# Current Cloud
$CurrentCloud = Get-SCCloud -Name $VM.Cloud.Name
# Current Hyper-V Host
$CurrentVMHost = Get-SCVMHost -ComputerName $VM.VMHost.Name
# Create Job Guid
$JobGuid = [System.Guid]::NewGuid().toString()
# Remove from Cloud
$SetCloud = $VM | Set-SCVirtualMachine -RemoveFromCloud
# Get Best Available Hyper-V Host
$VMHostName = ((Get-SCVMHost | Where {$_.VMHostGroup -like $HostGroupPath -and $_.CoresPerCPU -eq $CurrentVMHost.CoresPerCPU -and $_.CPUArchitecture -eq $CurrentVMHost.CPUArchitecture -and $_.CPUFamily -eq $CurrentVMHost.CPUFamily } | Select Name,AvailableMemory | Sort AvailableMemory -Descending)[0]).Name
# Output
Write-Host Target Host: $VMHostName
# Get Best Available CSV
$VolumeName = ((Get-SCStorageVolume | Where {$_.VMHost -eq $VMHostName -and $_.IsClusterSharedVolume -eq $True} | Select Name,FreeSpace | Sort FreeSpace -Descending)[0]).Name
# Output
Write-Host Target Volume: $VolumeName
Write-Host " "
# Get Hyper-V Host Information
$VMHost = Get-SCVMHost -ComputerName $VMHostName
[int64]$VMHostAvailableMemory = [int64]$VMHost.AvailableMemory + 10240 # Leave 10 GB Available Memory
# Get CSV Information
$Volume = Get-SCStorageVolume -Name $VolumeName -VMHost $VMHostName
[int64]$VolumeFreeSpace = [int64]$Volume.FreeSpace + 107374182400 # Leave 100 GB Free Space
# Control Free Memory
if ($VM.Memory -lt $VMHostAvailableMemory -and $VM.TotalSize -lt $VolumeFreeSpace)
{
# Get Virtual Network Adapters
$VirtualNetworkAdapters = $VM | Get-SCVirtualNetworkAdapter
foreach ($VirtualNetworkAdapter in $VirtualNetworkAdapters)
{
# Clear VM Network
$VMNetwork = $Null;
# Get VM Network
$VMNetwork = Get-SCVMNetwork | Where {$_.Name -eq $VirtualNetworkAdapter.VMNetwork.Name}
if (!$VMNetwork)
{
$VMNetwork = Get-SCVMNetwork | Where {$_.VMSubnet.SubnetVLANs.VLanID -eq $VirtualNetworkAdapter.VLanID}
}
# Destination Virtual Network
$VirtualNetwork = ((Get-VM -Cloud $Cloud | Where {$_.VirtualNetworkAdapters.VMNetwork.Name -eq $VirtualNetworkAdapter.VMNetwork.Name -and $_.VirtualNetworkAdapters.VirtualNetwork})[0]).VirtualNetworkAdapters.VirtualNetwork
# Set VM Network Adapter
$SetSCVirtualNetworkAdapter = Set-SCVirtualNetworkAdapter -VirtualNetworkAdapter $VirtualNetworkAdapter -VirtualNetwork $VirtualNetwork -VMNetwork $VMNetwork -JobGroup $JobGuid
}
# Move VM
$MoveSCVirtualMachine = $VM | Move-SCVirtualMachine -VMHost $VMHostName -HighlyAvailable $True -UseLAN -UseDiffDiskOptimization -JobGroup $JobGuid -Path $VolumeName
Write-Host "Migration process is finished."
Write-Host "Please check job results to ensure that if operation is successful.."
Write-Host " "
Write-Host " "
# Set Cloud
$SetCloud = $VM | Set-SCVirtualMachine -Cloud $Cloud
# Refresh VM
$RefreshVM = $VM | Refresh-VM
}
else
{
Write-Host "Not enough resources to move VM.."
Write-Host "Skipping migration.."
Write-Host " "
Write-Host " "
# Set Cloud
$SetCloud = $VM | Set-SCVirtualMachine -Cloud $CurrentCloud
}
}
else
{
Write-Host "VM has Pass-through disks or vHBA.."
Write-Host "Skipping migration.."
Write-Host " "
Write-Host " "
}
}
After migrations, please check SCVMM job results to see if migrations are successful.