Examples/Ravello-Import-vSphere-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
#requires -Version 3 -module Ravello

# Script will import a VM from a vSphere environment
# into your Ravello Image library
#
 
param(
    [Parameter(Mandatory=$true)]
    [string]$VmName,
    [Parameter(Mandatory=$true)]
    [string]$VMHost,
    [Parameter(Mandatory=$true)]
    [string]$EsxUser,
    [Parameter(Mandatory=$true)]
    [string]$EsxPassword
)

$PCLI = 'VMware.VimAutomation.Core'

# Load PowerCLI
Try
{
    Get-PSSnapin -Name $PCLI -ErrorAction Stop | Out-Null
}
Catch
{
    Add-PSSnapin -Name $PCLI
}

# Get credentials
$sEsxPswd = ConvertTo-SecureString -String $EsxPassword -AsPlainText -Force
$credEsx = New-Object System.Management.Automation.PSCredential ($EsxUser, $sEsxPswd)

Connect-VIServer -Server $VMHost -Credential $credEsx | Out-Null

# Get VMX path
$vm = Get-VM -Name $vmName
while($vm.PowerState -ne 'PoweredOff')
{
    Stop-VM -VM $vm -Confirm:$false
    sleep 5
}

# Remove existing Image
$img = Get-RavelloImage -ImageName $vmName
if($img)
{
    $img | Remove-RavelloImage -Confirm:$false
}

# Import VM
$sImport = @{
    EsxVmPath = $vm.ExtensionData.Config.Files.VmPathName
    EsxCredential = $credEsx
    EsxServer = $vm.ExtensionData.VMHost.Name
    Confirm = $false
}
Import-Ravello @sImport

Disconnect-VIServer -Server $VMHost -Confirm:$false

# Set VM as verified
$flag = '1stPass'
while('1stPass','PARSING' -contains $flag)
{
    $img = Get-RavelloImage -ImageName $vmName
    $flag = $img.loadingStatus
}

if($img.loadingStatus -ne 'DONE')
{
    $img | Update-RavelloImage -Description 'Imported from vSphere' -Confirm:$false
}