-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathWaitFor-VM.ps1
96 lines (85 loc) · 2.02 KB
/
WaitFor-VM.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
<#
.SYNOPSIS
Starts the specified VM and waits until it is ready for use.
.PARAMETER Name
The name of the VM to start.
.PARAMETER Restore
If specified then restore the latest snapshot of the VM.
#>
[CmdletBinding()]
param (
[Parameter(Position=0, Mandatory=$true, HelpMessage='Enter the name of the VM to start')]
[ValidateScript({
if ([bool](Get-VM $_ -ErrorAction SilentlyContinue)) { $true } else {
Throw "VM ""${_}"" not found"
}
})]
[string] $Name,
[switch] $Restore
)
Begin
{
function RestoreVM
{
$checkpoint = (Get-VMSnapshot $Name) | Sort -Descending CreationTime | Select -First 1
if ($checkpoint)
{
Write-Host ('... restoring snapshot {0}' -f $checkpoint.Name)
$global:ProgressPreference = 'SilentlyContinue'
$checkpoint | Restore-VMSnapshot -Confirm:$false
$global:ProgressPreference = 'Continue'
}
}
function StartVM
{
$vm = (Get-VM $Name)
if ($vm.State -ne 'Running')
{
Write-Host "... starting VM $Name"
$global:ProgressPreference = 'SilentlyContinue'
$vm = (Start-VM -Name $Name)
$global:ProgressPreference = 'Continue'
}
else
{
Write-Host "... VM $Name already running"
}
}
function WaitForHeartbeat
{
Write-Host '... detecting VM heartbeat'
[double] $wait = 0
While ((Get-VMIntegrationService -VMName $Name -Name 'Heartbeat').PrimaryStatusDescription -ne 'OK')
{
Start-Sleep -m 200
$wait += 200
}
Write-Host '... detecting VM IP address'
$ip = $null
While ([String]::IsNullOrWhiteSpace($ip))
{
Start-Sleep -m 200
$wait += 200
$vm = Get-VM -Name $Name
$ip = $vm.NetworkAdapters[0].IPAddresses | ? { $_.IndexOf('.') -gt 0 } | Select -First 1
}
$wait = $wait / 1000
Write-Host ("... VM $Name ($ip) stabilized after {0:0.00} seconds" -f $wait)
}
}
Process
{
if ($Restore)
{
if ((Get-VM $Name).State -eq 'Running')
{
Write-Host '... stopping VM'
$global:ProgressPreference = 'SilentlyContinue'
Stop-VM $Name -TurnOff -Force
$global:ProgressPreference = 'Continue'
}
RestoreVM
}
StartVM
WaitForHeartbeat
}