-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathMove-VMOffline.ps1
67 lines (52 loc) · 2.36 KB
/
Move-VMOffline.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
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$VMName,
[Parameter(Mandatory=$true)]
[string]$DestinationHost,
[Parameter(Mandatory=$true)]
[string]$CertificateThumbprint
)
$ErrorActionPreference = 'Stop'
if ((Get-VM $VMName).State -eq 'Running') {
throw "The virtual machine must be stopped to use this command."
}
# Remove current replication (if any)
Remove-VMReplication -VMName $VMName -ErrorAction SilentlyContinue
# Setup temporary replication to destination host
Enable-VMReplication -VMName $VMName -ReplicaServerName $DestinationHost -AuthenticationType Certificate -CertificateThumbprint $CertificateThumbprint -ReplicaServerPort 443 -CompressionEnabled $true
# Move VM files from Replica to Primary location
$sourceVhdx = (Get-VM $VMName -ComputerName $DestinationHost | Get-VMHardDiskDrive).Path
$targetVhdx = (Get-VM $VMName | Get-VMHardDiskDrive).Path
$targetVhdx = Join-Path (Split-Path $targetVhdx -Parent) (Split-Path $sourceVhdx -Leaf)
$vhds = @(@{'SourceFilePath' = $sourceVhdx; 'DestinationFilePath' = $targetVhdx})
Invoke-Command -ComputerName $DestinationHost {
Move-VMStorage -VMName $using:VMName -VirtualMachinePath 'C:\Hyper-V\Virtual Machines' -SnapshotFilePath 'C:\Hyper-V\Snapshots' -VHDs $using:vhds
}
# Replicate
Start-VMInitialReplication -VMName $VMName -AsJob |
Receive-Job -Wait
# Start Failover
Start-VMFailover -Prepare -VMName $VMName -Confirm:$false
Start-VMFailover -VMName $VMName -ComputerName $DestinationHost -Confirm:$false
# Promote Replica to Primary
Set-VMReplication -Reverse -VMName $VMName -ComputerName $DestinationHost -CertificateThumbprint $CertificateThumbprint
# Remove temporary replication
Remove-VMReplication -VMName $VMName
Remove-VMReplication -VMName $VMName -ComputerName $DestinationHost
# Connect VM to switch
Get-VMNetworkAdapter -ComputerName $DestinationHost -VMName $VMName |
Connect-VMNetworkAdapter -SwitchName 'SWITCH'
# Start VM
$vm = Start-VM -VMName $VMName -ComputerName $DestinationHost -Passthru
# Wait for VM
$heartbeatService = $vm | Get-VMIntegrationService -Name 'Heartbeat'
$vmOnline = $false
do {
Start-Sleep -Seconds 1
$vmOnline = $heartbeatService.PrimaryStatusDescription -eq 'OK'
} until ($vmOnline)
# If VM is responding on new server, remove the source VM (do not erase .VHDXs)
if ($vmOnline) {
Remove-VM -VMName $VMName -Force
}