Comprehensive data protection for all workloads
Post Reply
Vitaliy S.
Product Manager
Posts: 24256
Liked: 1863 times
Joined: Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov
Contact:

Re: OVF Support

Post by Vitaliy S. »

Hello Justin,

I haven't tried doing this, but as far as I know you may achieve that using vSphere Client -> Export capability. Also could you please elaborate why do you want to create an OVF file for Veeam server?

Thank you!

adrian.woodrup
Influencer
Posts: 14
Liked: 10 times
Joined: Nov 01, 2012 8:44 am
Full Name: Adrian Woodrup
Contact:

Re: OVF Support

Post by adrian.woodrup »

Hi,

Does anyone have an answer for this? The best method I have come up with so far is to use instant recovery to get the powered off VM into vShpere then run an export... is this the best way? If so does anyone have a Powershell script that can perform the VM mount after a sucessful backup in vSphere and the export from VMWare to OVF?

cheers,

Adrian

foggy
Veeam Software
Posts: 19463
Liked: 1767 times
Joined: Jul 11, 2011 10:22 am
Full Name: Alexander Fogelson
Contact:

Re: OVF Support

Post by foggy »

Adrian,

Not sure whether this is the optimal approach, but you can use this script to start Instant Recovery, then clone the instantly recovered VM to a vApp, and then export the vApp to an OVF.

Thanks.

adrian.woodrup
Influencer
Posts: 14
Liked: 10 times
Joined: Nov 01, 2012 8:44 am
Full Name: Adrian Woodrup
Contact:

Re: OVF Support

Post by adrian.woodrup »

Thanks Foggy, but when running the first script to start the instant recovery im getting the error:

Code: Select all

Start-VBRInstantRecovery : Cannot convert 'System.Object[]' to the type 'Veeam.
Backup.Core.COib' required by parameter 'RestorePoint'. Specified method is not
 supported.
At \\files\software\AdminTools\PowerShellScripts\Backup\Veeam-backup.ps1:6 char
:38
+ Start-VBRInstantRecovery -ResotePoint <<<<  $rp -VMName web-srv2a -Server $se
rver -ResourcePool $res -PowerUp $true -NICsEnabled $true
    + CategoryInfo          : InvalidArgument: (:) [Start-VBRInstantRecovery],
    ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Veeam.Backup.PowerShell.Co
   mmand.StartVBRInstantRecovery
It looks like it can not find the restore point... any ideas???

Adrian

foggy
Veeam Software
Posts: 19463
Liked: 1767 times
Joined: Jul 11, 2011 10:22 am
Full Name: Alexander Fogelson
Contact:

Re: OVF Support

Post by foggy »

Probably this is due to the typo discussed in that thread above: "-ResotePoint" (which was supposed to be fixed).

adrian.woodrup
Influencer
Posts: 14
Liked: 10 times
Joined: Nov 01, 2012 8:44 am
Full Name: Adrian Woodrup
Contact:

Re: OVF Support

Post by adrian.woodrup »

I have already tried that, its still giving me the error:

Code: Select all

Start-VBRInstantRecovery : Cannot convert 'System.Object[]' to the type 'Veeam.Backup.Core.COib' required by parameter
'RestorePoint'. Specified method is not supported.
At \\files\software\AdminTools\PowerShellScripts\Backup\StartIR.ps1:5 char:39
+ Start-VBRInstantRecovery -RestorePoint <<<<  $rp -VMName web-srv2a -Server $server -ResourcePool $res -PowerUp $true
-NICsEnabled $true
    + CategoryInfo          : InvalidArgument: (:) [Start-VBRInstantRecovery], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Veeam.Backup.PowerShell.Command.StartVBRInstantRecovery

foggy
Veeam Software
Posts: 19463
Liked: 1767 times
Joined: Jul 11, 2011 10:22 am
Full Name: Alexander Fogelson
Contact:

Re: OVF Support

Post by foggy »

Seems that you haven't changed the VM name ("web-srv2a" is just an example, you should put your name here..).

adrian.woodrup
Influencer
Posts: 14
Liked: 10 times
Joined: Nov 01, 2012 8:44 am
Full Name: Adrian Woodrup
Contact:

Re: OVF Support

Post by adrian.woodrup »

Already chaged that:

Code: Select all

$rp = Get-VBRBackup | where {$_.JobName -eq "SRVTST02"} | Get-VBRRestorePoint
$server = Get-VBRServer | where {$_.Name -eq "172.31.100.30"}
$res = Find-VBRResourcePool -Server ($server) | where {$_.Name -eq "Resources"}

Start-VBRInstantRecovery -VMName SRVTST02 -Server $server -ResourcePool $res -PowerUp $true -NICsEnabled $true
Error:

Thanks!

foggy
Veeam Software
Posts: 19463
Liked: 1767 times
Joined: Jul 11, 2011 10:22 am
Full Name: Alexander Fogelson
Contact:

Re: OVF Support

Post by foggy »

Ok, then it's time to contact technical support directly for further troubleshooting.

veremin
Product Manager
Posts: 17834
Liked: 1662 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: OVF Support

Post by veremin » 1 person likes this post

Just a little question - what can be seen after having done first two commands by simply putting $server in console?

Code: Select all

$rp = Get-VBRBackup | where {$_.JobName -eq "SRVTST02"} | Get-VBRRestorePoint
$server = Get-VBRServer | where {$_.Name -eq "172.31.100.30"}
$server
In the case of everything working properly you should see information regarding your server such as: Name, Type, etc. And it will give you a notice that everything is going on fine with $server.

Nevertheless, you might see either nothing in case of null object or two records related to one server, in case of you having added one server as standalone server and as a part of vcenter. Both of the situations can spill over into some problems.

So, if it were up to me, I would definitely look at this firstly.
Thanks.

adrian.woodrup
Influencer
Posts: 14
Liked: 10 times
Joined: Nov 01, 2012 8:44 am
Full Name: Adrian Woodrup
Contact:

Re: OVF Support

Post by adrian.woodrup » 1 person likes this post

Thanks Vladimir, this put me on the right track! The process I have configured is to backup the vm using veeam, then run a post script to restore the vmdk files, then use the ovftool.exe (provided by vmware (google it)) to convert the vmdk's to ovf. It works well. here is the script:

# Get the job name and the last "restore point"
$rp = Get-VBRBackup -Name SRVTST02 | Get-VBRRestorePoint | sort CreationTime -Descending | select -First 1

# Get the local server name
$server = Get-VBRServer | where {$_.Name -eq "This server"}

# Set the job server name
$servername = "SRVTST02"

# Restores the VM's file to a temporary location
Start-VBRRestoreVMFiles -RestorePoint $rp -Server $server -Path "T:\$servername\"

# Opens the OVF convert tool, maps to the location of the vmx file and creates the OVF file in a new location
& "c:\Program Files\VMWare\VMWare OVF Tool\ovftool.exe" "T:\$servername\srvtst02.vmx" "d:\OVF-Export\$servername\SRVTST02.ovf"

# Delete the restored VM files
Del "R:\$servername"

veremin
Product Manager
Posts: 17834
Liked: 1662 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: OVF Support

Post by veremin »

Thank you for sharing your solution!

adrian.woodrup
Influencer
Posts: 14
Liked: 10 times
Joined: Nov 01, 2012 8:44 am
Full Name: Adrian Woodrup
Contact:

[MERGED] Change disk "createType" to streamOptimized in vm

Post by adrian.woodrup »

hi,

Im not sure if this is something Veeam can do or not but...

I am attempting to backup a running VM, then once the backup has completed a script runs restoring the VMs files, I then want convert these files to ovf using vmware ovf tool but to do this I need the disk.vmdk file to have the "createType=streamOptimized" not the VMWare "createType="vmfs"" is there a way to do this during the backup or restore process?

I have an open case with Veeam (00157798) but thought I would see if anyone else has come across this issue.

Many thanks,

Adrian

Vitaliy S.
Product Manager
Posts: 24256
Liked: 1863 times
Joined: Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov
Contact:

Re: Change disk "createType" to streamOptimized in vm file r

Post by Vitaliy S. »

Hi Adrian, no, that's not possible to do with Veeam. It might be better to search through the VMware Community forums for a PowerCLI script that can do it for you.

adrian.woodrup
Influencer
Posts: 14
Liked: 10 times
Joined: Nov 01, 2012 8:44 am
Full Name: Adrian Woodrup
Contact:

Re: OVF Support

Post by adrian.woodrup » 2 people like this post

Took me a while but I finally worked this one out, its a long winded process but does work.. code is here:

Code: Select all

# Script to run after each backup is performed. need to install the VMWare OVF tool and the VMWare vDisk tool
# Set each parameter below as required
# "global:JobName" is the "Name of the Veeam backup job that contains the server"
# "global:servername" is the "Name of the server(s) you want to transfer to AWS"
# "Global:Disk" is the "Source of the temporary disk where the backup takes place (this can be either a drive letter of CIFS)". this veriable has been added incase the disk needs to change as it fills.
# "Global:NoOfServers" is the "the number of servers in the restore job" 

$global:JobName = "VeeamJobName"
$global:servername = "Server"
$Global:Disk = "DriveLetter"


# Static Parameters - you should not need to change these. 
# adds the Veeam PowerShell tools
add-PSSnapin VeeamPSSnapIn
# sets the restore point (RP) as well as the server and the name of the backup job. 
# This is done by setting the "-Name" to the Veeam back job name, then setting the server name and sorting by creation time, the first server in this list (i.e. the lastest one) is selected. 
$global:rp1 = Get-VBRBackup -Name $JobName | Get-VBRRestorePoint -Name $servername | sort CreationTime -Descending | select -First 1
# This sets the Veeam backup server, it is requried when performing a restore. As the server is local it looks at "this server" (not localhost) 
$global:server = Get-VBRServer | where {$_.Name -eq "This server"}
# Veriable to set this name and location of the restored vmdk files. During the conversion process the vmdks are deleted, renamed and moved. 
$global:OriginalFile = "$Disk\$servername\$servername.vmdk"
# Veriable to set the location and name of the temporary converted disk. the VMWare disk conversion applciation MUST convert the disk to a different name to run correctly. 
$global:ConvertedDisk = "$Disk\$servername\converted_$servername.vmdk"
# Veriable to set the location and name of the OVF file. This is used by the OVF converter and moves the OVF to a disk that is replicated to the cloud storage
$global:OVFFile = "R:\VMWARE\$servername\OVF\$servername.ovf"
# Set the $date veriable to the current date and time. required for the logs
$global:date = (get-date)
# veriable to specify the log file location and puts the date and time at the being of the file name
$global:log = ("d:\Logs\VMWare\{0:dd-MM-yyyy} SRV07 OVF to AWS.log" -f (get-date))

# Commands
# Convert server 
# Veeam PS tool to run the VMWare file restore. The path is added as teh server name and will be deleted on completetion. 
Start-VBRRestoreVMFiles -RestorePoint $rp -Server $server -Path "$Disk\$servername\"

# Convert the restored disks to a generic virtual format. Runs a check to confirm the servers VMDK files have been restored, then converts the disk to a generic VMDK
$CheckRestore = (Test-Path $OriginalFile)
	if ($CheckRestore){
		& "d:\program files (x86)\VMware\VMware Virtual Disk Development Kit\bin\.\vmware-vdiskmanager.exe" -r $OriginalFile -t 5 $ConvertedDisk 
		}

# Checks the disk has been converted and removes the Veeam restored files. If the disk (vmdk file) hasnt been converted log this in the log file
$ConvertDisk = (Test-Path $ConvertedDisk)
	if ($ConvertDisk){
		remove-item $OriginalFile -force
		}
	else{
		echo "$date $servername Failed to Convert Disk to Generic format" | out-file -append $log
		}
# Check the conversion has been successful and if so rename the disk back to the original server name.VMDK
$Checkdisk = (test-path $ConvertedDisk)
	if($CheckDisk){
		rename-item $ConvertedDisk $Originalfile
		}

# Checks if the converted disk is present and converts it to OVF format on the replicated disk 
$ConvertDisk = (Test-Path $Originalfile)
	if ($ConvertDisk){
		& "d:\Program Files\VMWare\VMWare OVF Tool\ovftool.exe" --targetType=OVF "$Disk\$servername\$servername.vmx" $OVFFile
		}

# check to see if the conversion was successful and write this to a log 
$OVF = (Test-path $OVFFile)
	if ($OVF){
		 echo "$date $servername successfully converted to OVF" | out-file -append $log
		 }
	Else{
		echo "$date $servername failed to convert to OVF" | out-file -append $log
		}
$OVF = (Test-path $OVFFile)
	if ($OVF){
		 Remove-Item "$disk\$servername" -force
		 }


#################################################################
#		Check backup status and send notification				#
#################################################################
# Check and notification paramaters
# Backup failed - check for words "did not copy" in the log file
$BackupFailed = Get-Content $log | Select-String "failed" -quiet
# Backup completed successfully - check for words "copied ok"
$BackupSuccess = Get-Content $log | Select-String "successfully" -quiet
# Set the SMTP server
$PSEmailserver = 'email.company.com' 

# If the backup failed send a failure email with the log file attached and the failure message
if ($BackupFailed) {
	Send-MailMessage -To 'user@company.com' -From 'OVF@company.com' -Subject "DR/Backup of $servername failed" -attachments $Log -Body "The replication of $servername to the cloud failed. Check the logs to confirm why the backup failed!"
} 
# If the backup was successful send a success email with the log file and message
elseif ($BackupSuccess){
	Send-MailMessage -To 'user@company.com' -From 'OVF@company.com' -Subject "DR/Backup of $servername successful" -attachments $Log -Body "The DR/Backup of $servername was successful! Attached log file has all details."  
} 
# if there wasnt a log file created or nothing was in the log file send this message
else {
	Send-MailMessage -To 'user@company.com' -From 'OVF@company.com' -Subject "DR/Backup of $servername failed" -attachments $Log -Body "The replication of $servername to the cloud failed. Check the logs to confirm why the backup failed!"
}
any questions feel free to ask.

Gostev
SVP, Product Management
Posts: 26701
Liked: 4276 times
Joined: Jan 01, 2006 1:01 am
Location: Baar, Switzerland
Contact:

Re: OVF Support

Post by Gostev »

Fantastic! Thanks a lot for sharing.

tsightler
VP, Product Management
Posts: 5679
Liked: 2497 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: OVF Support

Post by tsightler »

Yes, thanks much for this script, it's great. Because I had a few clients that were looking to do something very similar, and the VMs were somewhat large, I also have a script to do this using the Instant Recovery method as mentioned earlier in this thread, which has the advantage of not requiring the extra space since it's streamed from vPowerNFS.

There are two small "gotchas" with this script, one, I could not figure out how to perform an instant recovery via PowerShell except to an ESXi host that has been added as "standalone" to Veeam. Any attempts to do this otherwise seems to give an authentication error. Not a big deal, but you'll have to add at least one host to Veeam as a standalone host with credentials. It can be a host that's also controlled by vCenter, but you should add it as standalone via IP address, and then use the IP in the script. If there is a way to use Start-VBRInstantRecovery with vCenter, someone please let me know.

The other small gotcha is that, when performing the Instant VM Recovery, I have to use an alternate VM name, so the name of the VM in the OVF include "_ovf" on the end of the VM name. This wasn't an issue for the use case I was going for since you can always set the VM name during import of the OVF anyway.

Code: Select all

asnp VMware.VimAutomation.Core -ErrorAction SilentlyContinue
asnp VeeamPSSnapin -ErrorAction SilentlyContinue

# Name of Veeam Backup Job
$VeeamJob = "<Veeam_Job>"
# Name of VM to Convert
$VmName = "<VM_Name>"
# Name of vCenter to Export VM From
$VcName = "<VC_Name>"
# Name of ESX host for Instant VM Restore
$EsxHost = "<ESX_Host>"
# Destination Folder for OVF
$Dest = "<Path to Folder>"
# Path to OVF Tool on your system
$OVFTool = "C:\Program Files\VMware\VMware OVF Tool\ovftool.exe"

# Temp name for Instant Recovery VM
$VmIRName = $VmName + "_ovf"
# Find most recent restore point for VM
$ExportObj = (Get-VBRBackup -name "$VeeamJob").GetLastOibs() | Where-Object {$_.Name -eq $VmName}

# Get ESX Server and Resource Pool and start Instant Recovery with VM powered off
$EsxObj = Get-VBRServer -Name "$EsxHost"
$ResourcePoolObj = Find-VBRResourcePool -Server $EsxObj -Name "Resources" 
Start-VBRInstantRecovery -RestorePoint $ExportObj -VMName $VmIRName -Server $EsxObj -ResourcePool $ResourcePoolObj -PowerUp $false -NICsEnabled $true

# Connect to vCenter and get the Instant Recovery VM
Connect-VIServer $VCName -AllLinked
$VmIR = Get-VM -Name "$VmIRName"

# Get session and ticket
$Session = Get-View -Id Sessionmanager
$Ticket = $Session.AcquireCloneTicket()

# Call OVFtool to 
& $OVFTool "--name=$VmName" "--noSSLVerify" "--targetType=OVF" "--I:sourceSessionTicket=$($Ticket)" "vi://$($VcName)?moref=vim.VirtualMachine:$($VmIR.extensiondata.moref.value)" "$($Dest)$($VMName).ovf"

# Remove VM from vCenter Inventory and Stop Instant VM Recovery
Remove-VM $VmIR -Confirm:$false
Stop-VBRInstantRecovery (Get-VBRInstantRecovery | where-object {$_.VMName -eq "$VMIRName"})

adrian.woodrup
Influencer
Posts: 14
Liked: 10 times
Joined: Nov 01, 2012 8:44 am
Full Name: Adrian Woodrup
Contact:

Re: OVF Support

Post by adrian.woodrup »

I couldnt get the instant recovery to work but sounds like adding the host directly would get around the issue I was having, thanks I will edit the script as that will speed up the conversions a lot!

adrian.woodrup
Influencer
Posts: 14
Liked: 10 times
Joined: Nov 01, 2012 8:44 am
Full Name: Adrian Woodrup
Contact:

Re: OVF Support

Post by adrian.woodrup »

Script works perfectly, I did have to make a few changes to get it working and I have also added the notification in. For ease I have stored the user account and password in this one.

Code: Select all

asnp VMware.VimAutomation.Core -ErrorAction SilentlyContinue
asnp VeeamPSSnapin -ErrorAction SilentlyContinue

# Name of Veeam Backup Job
$VeeamJob = "VeeamJobName"
# Name of VM to Convert
$Server = "VMName"
# Name of vCenter to Export VM From
$VcName = "vCenterServerName"
# Name of ESX host for Instant VM Restore (this is either the host's name or IP, if the host is shown in veeam under vcenter as IP then use name, if name use IP) 
$EsxHost = "ESXNameOrIP"
# Destination Folder for OVF
$Dest = "drive:\Path\VMWARE\OVF\"
# Path to OVF Tool on your system
$OVFTool = "D:\Program Files\VMware\VMware OVF Tool\ovftool.exe"
# User accoun that has access to vCentre
$user = 'username'
# password of the account
$password = 'password' 
# Date
$Date = get-date -Format ddMMyyyy
# Path of Log file
$Log = "D:\logs\VMWare\$date $Server OVF to AWS.log"
# Email recipient
$EmailTo = 'emailaddress'
# Email sender address
$EmailFrom = 'emailaddress'

# Temp name for Instant Recovery VM
$VmIRName = $Server + "_ovf"
# Find most recent restore point for VM
$ExportObj = (Get-VBRBackup -name "$VeeamJob").GetLastOibs() | Where-Object {$_.Name -eq $Server}

# Get ESX Server and Resource Pool and start Instant Recovery with VM powered off
$EsxObj = Get-VBRServer -Name "$EsxHost"
$ResourcePoolObj = Find-VBRResourcePool -Server $EsxObj -Name "Resources" 
Start-VBRInstantRecovery -RestorePoint $ExportObj -VMName $VmIRName -Server $EsxObj -ResourcePool $ResourcePoolObj -PowerUp $false -NICsEnabled $true

# Connect to vCenter and get the Instant Recovery VM
Connect-VIServer $VCName -User $user -Password $password
$VmIR = Get-VM -Name "$VmIRName"

# Get session and ticket
$Session = Get-View -Id Sessionmanager
$Ticket = $Session.AcquireCloneTicket()

# Call OVFtool to 
& $OVFTool "--name=$Server" "--noSSLVerify" "--targetType=OVF" "--I:sourceSessionTicket=$($Ticket)" "vi://$($VcName)?moref=vim.VirtualMachine:$($VmIR.extensiondata.moref.value)" "$($Dest)$($Server).ovf"

# Remove VM from vCenter Inventory and Stop Instant VM Recovery
$IR = Get-VBRInstantRecovery | where-object {$_.VMName -eq "$VMIRName"}
Stop-VBRInstantRecovery $IR
Remove-VM $VmIR -Confirm:$false 

#Check OVF has completed
$OVFFile = "$($Dest)$($Server).ovf"
If ($OVFFile){write-host "$Server OVF conversion successfully completed" | out-file -append $log}
else {write-host "$Server OVF conversion failed to complete" | out-file -append $log}

#################################################################
#		Check backup status and send notification				#
#################################################################

# Email server
$PSEmailserver = 'sophosmail' 
$log
$Failed = Get-Content $log | Select-String "failed" -quiet
$Success = Get-Content $log | Select-String "successfully" -quiet

 
# Backup completed successfully - check for words "copied ok"
if ($BackupFailed){$Subject = "$Server OVF backup failed"}
else {$subject = "$Server OVF backup Success"}

# If the backup failed send a failure email with the log file attached and the failure message
if ($Failed) {
	Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $Subject -attachments $log -Body "The $Server file backup failed. Check the logs on SRV21 to confirm why the backup failed!"
} 
elseif ($Success) {
	Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $Subject -attachments $log -Body "The $Server file backup was successful!"
}
else {
	Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $Subject -attachments $log -Body "The $Server OVF backup failed!"
}

tsightler
VP, Product Management
Posts: 5679
Liked: 2497 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: OVF Support

Post by tsightler »

Cool. I'm curious, how the performance of this method compares to the original method you were using? It seems that ovftool is pretty slow pulling from vPower NFS, but I thought it would probably still be faster than having to stage.

adrian.woodrup
Influencer
Posts: 14
Liked: 10 times
Joined: Nov 01, 2012 8:44 am
Full Name: Adrian Woodrup
Contact:

Re: OVF Support

Post by adrian.woodrup »

The VM file restore could take a while, and if converting from a vmdk file (from a file level restore) and not from a VM in vcenter I had to first convert the disk to a generic format (using vmware-vdiskmanager.exe) before the OVF tool would work. All in all a 75GB vm could take up to (and sometimes over) 8 hours. With the new method I have converted the same VM in 2 hours 20 mins, give or take.

Thanks for the help!

adrian.woodrup
Influencer
Posts: 14
Liked: 10 times
Joined: Nov 01, 2012 8:44 am
Full Name: Adrian Woodrup
Contact:

Re: OVF Support

Post by adrian.woodrup » 5 people like this post

After days and days of it sporadically failing, crashing, not disconnecting the OVF from VMware and various other issues (that almost had me cursing the day i met Powershell :D ) I have finally created a script that has so far (one week) been bullet proof... If you have any questions please feel free to ask.

Code: Select all

# This script runs a different OVF conversion depending on the Day of the week.
$VeeamJob = "JobName"
$server1 = 'ServerName'

asnp VMware.VimAutomation.Core -ErrorAction SilentlyContinue
asnp VeeamPSSnapin -ErrorAction SilentlyContinue

$Dest1 = "R:\VMWARE\OVF\$Server1\"

$OVFFile1 = "$($Dest1)$($Server1).ovf"

$VmIRName1 = $Server1 + "_ovf"

# Name of vCenter to Export VM From
$VcName = "vCenterServer"
# Name of ESX host for Instant VM Restore
$EsxHost = "SingleESXHost"
# Path to OVF Tool on your system
$OVFTool = "D:\Program Files\VMware\VMware OVF Tool\ovftool.exe"
# Date
$Date = get-date -Format yy-MM-dd
$TimeStamp = get-date 
# Path of Log file
$Log = "D:\logs\VMWare\" + $Date + ' ' + $Server1 + " OVF to AWS.log"
# Email sender address
$EmailTo = 'Address@Domain.com'
$EmailFrom = 'Address@Domain.com'

#############################################################################################
$error.clear()
Add-content -Path $Log -Value "Backup of Server $Server1 Started at $TimeStamp"
# Find most recent restore point for VM
$ExportObj1 = (Get-VBRBackup -name "$VeeamJob").GetLastOibs() | Where-Object {$_.Name -eq $Server1}

# Get ESX Server and Resource Pool and start Instant Recovery with VM powered off
$EsxObj1 = Get-VBRServer -Name "$EsxHost"
$ResourcePoolObj = Find-VBRResourcePool -Server $EsxObj1 -Name "Resources" 
Start-VBRInstantRecovery -RestorePoint $ExportObj1 -VMName $VmIRName1 -Server $EsxObj1 -ResourcePool $ResourcePoolObj -PowerUp $false -NICsEnabled $true
if (!$?) {
	Add-Content -Path $Log -Value "$server1 InstantRecovery failed with Error (error code:0x00000080): $error"
	Add-Content -Path $Log -Value "Stopping the backup of $Server1"
	Break}
Else{
	Add-Content -Path $Log -Value "$Server1 InstantRecovery Started successfully..."
	# Connect to vCenter and get the Instant Recovery VM
	Connect-VIServer $VCName -User 'ESXAdminUser' -Password 'Password' 
	if (!$?) {
		Add-Content -Path $Log -Value "$server1 Connection to vCenter failed with Error (error code:0x00000079): $error"
		Add-Content -Path $Log -Value "Stopping the backup of $Server1"
		Break}
	Else{
		Add-Content -Path $Log -Value "$Server1 Connection to VI Server was successful..."
		$VmIR1 = Get-VM -Name "$VmIRName1"
		# Get session and ticket
		$Session = Get-View -Id Sessionmanager
		$Ticket = $Session.AcquireCloneTicket()
		# Call OVFtool to 
		& $OVFTool "--name=$Server1" "--noSSLVerify" "--targetType=OVF" "--overwrite" "--I:sourceSessionTicket=$($Ticket)" "vi://$($VcName)?moref=vim.VirtualMachine:$($VmIR1.extensiondata.moref.value)" "$($Dest1)$($Server1).ovf"
		if (!$?) {
			Add-Content -Path $Log -Value "$server1 OVF conversion and transfer failed with Error (error code:0x00000078): $error"
			Add-Content -Path $Log -Value "Stopping the backup of $Server1"
			Break}
		Else{
			Add-Content -Path $Log -Value "$Server1 OVF conversion and transfer completed successfully..."
			# Remove VM from vCenter Inventory and Stop Instant VM Recovery
			$IR1 = Get-VBRInstantRecovery | where-object {$_.VMName -eq "$VMIRName1"}
			Stop-VBRInstantRecovery $IR1
			if (!$?) {
				Add-Content -Path $Log -Value "$server1 Stop InstantRecovery failed with Error (error code:0x0000077): $error"
				Add-Content -Path $Log -Value "Stopping the backup of $Server1"
				Break}
			Else{
				Add-Content -Path $Log -Value "$Server1 stopping of the Instant Recovery completed successfully..."
				Remove-VM $VmIR1 -Confirm:$false 
				if (!$?) {
					Add-Content -Path $Log -Value "$server1 Removal of Server_OVF Failed with Error (error code:0x00000076): $error"
					Add-Content -Path $Log -Value "Stopping the backup of $Server1"
					Break}
				Else{
					Add-Content -Path $Log -Value "$Server1 Removeal of the temp OVF completed successfully..."
					# disconnect from vCenter
					Disconnect-VIServer $VCName -Confirm:$False
					if (!$?) {
						Add-Content -Path $Log -Value "$server1 failed to disconnect from the vCenter server with Error (error code:0x00000075): $error"
						Add-Content -Path $Log -Value "Stopping backup of $Server1"
						Break}
					Else{
						Add-Content -Path $Log -Value "$Server1 OVF conversion and transfer completed successfully (OK) at $TimeStamp..."
						
					}
				}
			}
		}
	}
}

#################################################################
#		Check backup status and send notification				#
#################################################################
#Check OVF has completed
$Failed = Get-Content $log | Select-String "0x000000" -quiet
$Success = Get-Content $log | Select-String "(OK)" -quiet

# Email server
$PSEmailserver = 'EmailServer' 
# Email subject depends on the log file, if there is an error in the log file set subject to failed. if there is an OK in the log set to success, otherwise set to unknown
If ($Failed){$Subject = $Server1 + ' OVF backup failed'}
Elseif ($Success) {$subject = $Server1 + ' OVF backup Success'}
else {$subject = "UNKNOWN OVF backup Result"}

# Send an email with details. 
Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $Subject -attachments $log -Body "See the log file for the results of the OVF backup of $Server1"
I created my own error codes so first line could easily identify the failure and update the incident with exactly what part of the conversion it failed on.

joergr
Expert
Posts: 386
Liked: 39 times
Joined: Jun 08, 2010 2:01 pm
Full Name: Joerg Riether
Contact:

Re: OVF Support

Post by joergr »

Adrian, this is extremely SWEET!! Thanks a lot!

Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests