-
Notifications
You must be signed in to change notification settings - Fork 6
/
Move-VMCold.ps1
82 lines (49 loc) · 2.38 KB
/
Move-VMCold.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
function Move-VMCold {
[CmdletBinding(SupportsShouldProcess = $true,ConfirmImpact = 'High')]
param (
[parameter(position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyname=$True,Mandatory=$True)]
[VMware.VimAutomation.ViCore.types.V1.Inventory.VirtualMachine[]]
$VM,
[Parameter(Mandatory=$true)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost]
$Destination,
[validateset("true","false")]
[string]
$PowerON="true"
)
Begin {
$HostVlan = ($Destination | get-virtualportgroup).name
$hostDS = ($Destination | get-datastore).name
}
Process {
$VM | ForEach-Object {
$currentVM = $_
$VMVlan = ($currentVM | get-virtualportgroup).name
$VMDS = ($currentVM | get-datastore).name
$Problems = @()
$VMVlan | ForEach-Object { IF ($HostVlan -notcontains $_) {$Problems += $_} }
$VMDS | ForEach-Object { IF ($hostDS -notcontains $_) {$Problems += $_} }
IF ($PSCmdlet.ShouldProcess($currentVM.name,"Shut down and Move to $($Destination.name)") -and !$Problems) {
TRY {
IF ($currentVM.PowerState -eq "PoweredOn") {
Write-Verbose "Shutting down guest os on $($currentVM.name)"
$currentVM | Stop-VMGuest -Confirm:$False | Out-Null
Write-Warning "Waiting on $($currentVM.name) for PowerOff state"
While ((Get-vm $currentVM).PowerState -ne "PoweredOff") {
Write-Verbose "Wait for PowerOff state"
sleep 2
}
}
Write-Verbose "vMotion $($currentVM.name) to $($Destination.name)"
Move-VM -Destination $Destination -VM $currentVM -Confirm:$False | Out-Null
IF ($PowerON -eq "True") {
Write-Verbose "Restart $($currentVM.name) on $($Destination.name)"
Start-VM $currentVM
}
}
CATCH {Write-Error $_.Exception -ErrorAction Continue}
} ELSEIF ($Problems) {Write-error "The following objects were not found on destination: $Problems"} #IF PScmdlet and $Problems
Clear-Variable VMVlan,VMDS,Problems
}#foreach VM
}#process
}