Hi Team,
Need a powercli script to keep the MAC address when we export the vm to OVF file.
From webclient we have the option to keep MAC address while exporting to ovf (please find the attachments) but not able to find it using powercli.
Powercli script tried:
Export-VM -VM $vm -Format Ovf -Destination "C:\ovf" -Name MyVM-ovf -Force
Kindly help
Regards,
Chethan Kumar
VMware SDK Developer Support
Try this slightly adapted version.
#
# When in vSphere 6.5 ==> connect to the ESXi node
#
$vmName = 'TestVM'
$ovfPath = 'C:\OVFFolder'
# Advanced export options
$includeMac = $true
$includeUuid = $true
$includeExtraConfig = $true
$si = Get-View ServiceInstance
$ovfMgr = Get-View -Id $si.Content.OvfManager
$vm = Get-VM -Name $vmName
$lease = Get-View -Id ($vm.ExtensionData.ExportVm())
$files = @()
while($lease.State -eq [VMware.Vim.HttpNfcLeaseState]::initializing){
sleep 1
$lease.UpdateViewData('State')
}
$lease.UpdateViewData()
if($lease.State -eq [VMware.Vim.HttpNfcLeaseState]::ready){
Write-Host "Lease timeout $($lease.Info.LeaseTimeout)"
$bufferSize = 10KB
$Timeout = 300
$bytesWritten = 0
foreach($device in ($lease.Info.DeviceUrl | where{$_.Disk})){
Write-Host "Transferring $($device.Url.Split('/')[-1])"
$ovfFile = "$($ovfPath)\$($device.Url.Split('/')[-1].Replace('disk',$vmName))"
$url = $device.Url.Replace('*',$vm.VMHost.Name)
$webRequest = [System.Net.WebRequest]::Create($url)
$response = $webRequest.GetResponse()
$responseStream = $response.GetResponseStream()
$fileStream = [System.IO.File]::OpenWrite($ovfFile)
$chunk = New-Object byte[] $bufferSize
while (($bytesRead = $responseStream.Read($chunk,0,$bufferSize)))
{
$totalBytesRead += $bytesRead
$fileStream.Write($chunk,0,$bytesRead)
$fileStream.Flush()
}
$bytesWritten += $fileStream.Length
$ovfFileObj = New-Object VMware.Vim.OvfFile
$ovfFileObj.DeviceId = $device.Key
$ovfFileObj.Path = $device.Url.Split('/')[-1]
$ovfFileObj.Size = $fileStream.Length
$files += $ovfFileObj
$FileStream.Close()
$responseStream.Close()
$lease.HttpNfcLeaseProgress([int]($bytesWritten/($lease.Info.TotalDiskCapacityInKB * 1KB) *100))
Write-Host "Bytes written $($bytesWritten)"
}
$cdp = New-Object VMware.Vim.OvfCreateDescriptorParams
$cdp.Description = 'Test OVF Export'
$cdp.Name = "$($vmName)"
if($includeMac){
$cdp.ExportOption += 'mac'
}
if($includeUuid){
$cdp.ExportOption += 'uuid'
}
if($includeExtraConfig){
$cdp.ExportOption += 'extraconfig'
}
$cdp.OvfFiles = $files
$ovfResult = $ovfMgr.CreateDescriptor($vm.ExtensionData.MoRef,$cdp)
$xmlOvfDescriptor = ([xml]$ovfResult.OvfDescriptor)
$xmlOvfDescriptor.Envelope.References.File | %{
$_.href = $_.href.Replace('disk',$vmName)
}
$xmlOvfDescriptor.Save("$($ovfPath)\$($vmName).ovf")
$lease.HttpNfcLeaseProgress(100)
$lease.HttpNfcLeaseComplete()
Write-Host "Export complete"
}
else{
Write-Host "Export failed"
Write-Host $lease.Error[0].LocalizedMessage
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
As you noticed, the Export-vApp cmdlet doesn't have that option.
You will have to revert to the ExportVM method if you want to use that option.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I tried with ExportVm method but not able to find.
Could you please help me with this query.
Regards,
Chethan Kumar
Let me see if I can come up with something
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I have thrown together something quick and dirty that seems to work.
When you are on vSPhere 6.5, you will need to connect (Connect-VIServer) to the ESXi node that hosts the VM that you want to export.
The MAC, and some other options can be selected via some Boolean values at the beginning of the script.
Give it a try, and let me know if it works,.
If all works well, I can convert this to a more official looking function :smileygrin:
#
# When in vSphere 6.5 ==> connect to the ESXi node
#
$vmName = 'TestVM'
$ovfPath = 'C:\OVFFolder'
# Advanced export options
$includeMac = $true
$includeUuid = $true
$includeExtraConfig = $true
$si = Get-View ServiceInstance
$ovfMgr = Get-View -Id $si.Content.OvfManager
$vm = Get-VM -Name $vmName
$lease = Get-View -Id ($vm.ExtensionData.ExportVm())
$files = @()
while($lease.State -eq [VMware.Vim.HttpNfcLeaseState]::initializing){
sleep 1
$lease.UpdateViewData('State')
}
$lease.UpdateViewData()
if($lease.State -eq [VMware.Vim.HttpNfcLeaseState]::ready){
Write-Host "Lease timeout $($lease.Info.LeaseTimeout)"
$bufferSize = 10KB
$Timeout = 300
$bytesWritten = 0
foreach($device in ($lease.Info.DeviceUrl | where{$_.Disk})){
Write-Host "Transferring $($device.Url.Split('/')[-1])"
$ovfFile = "$($ovfPath)\$($device.Url.Split('/')[-1])"
$url = $device.Url.Replace('*',$vm.VMHost.Name)
$webRequest = [System.Net.WebRequest]::Create($url)
$response = $webRequest.GetResponse()
$responseStream = $response.GetResponseStream()
$fileStream = [System.IO.File]::OpenWrite($ovfFile)
$chunk = New-Object byte[] $bufferSize
while (($bytesRead = $responseStream.Read($chunk,0,$bufferSize)))
{
$totalBytesRead += $bytesRead
$fileStream.Write($chunk,0,$bytesRead)
$fileStream.Flush()
}
$bytesWritten += $fileStream.Length
$ovfFileObj = New-Object VMware.Vim.OvfFile
$ovfFileObj.DeviceId = $device.Key
$ovfFileObj.Path = $device.Url.Split('/')[-1]
$ovfFileObj.Size = $fileStream.Length
$files += $ovfFileObj
$FileStream.Close()
$responseStream.Close()
$lease.HttpNfcLeaseProgress([int]($bytesWritten/($lease.Info.TotalDiskCapacityInKB * 1KB) *100))
Write-Host "Bytes written $($bytesWritten)"
}
$cdp = New-Object VMware.Vim.OvfCreateDescriptorParams
$cdp.Description = 'Test OVF Export'
$cdp.Name = "$($vmName)"
if($includeMac){
$cdp.ExportOption += 'mac'
}
if($includeUuid){
$cdp.ExportOption += 'uuid'
}
if($includeExtraConfig){
$cdp.ExportOption += 'extraconfig'
}
$cdp.OvfFiles = $files
$ovfResult = $ovfMgr.CreateDescriptor($vm.ExtensionData.MoRef,$cdp)
([xml]$ovfResult.OvfDescriptor).Save("$($ovfPath)\$($vmName).ovf")
$lease.HttpNfcLeaseProgress(100)
$lease.HttpNfcLeaseComplete()
Write-Host "Export complete"
}
else{
Write-Host "Export failed"
Write-Host $lease.Error[0].LocalizedMessage
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi,
Apologize for delayed response as we had some lab issues due to migration.
Thank you sharing script. The script is working fine.
we noticed is that with the browser export the vmdk is the same name as the VM, however, with the powercli script the vmdk is named disk-0.vmdk. If possible we would like the PowerCLI script to name the volumes with the same name as the VM just as the browser export performs.
Is this possible?
Regrads,
Chethan Kumar
Try this slightly adapted version.
#
# When in vSphere 6.5 ==> connect to the ESXi node
#
$vmName = 'TestVM'
$ovfPath = 'C:\OVFFolder'
# Advanced export options
$includeMac = $true
$includeUuid = $true
$includeExtraConfig = $true
$si = Get-View ServiceInstance
$ovfMgr = Get-View -Id $si.Content.OvfManager
$vm = Get-VM -Name $vmName
$lease = Get-View -Id ($vm.ExtensionData.ExportVm())
$files = @()
while($lease.State -eq [VMware.Vim.HttpNfcLeaseState]::initializing){
sleep 1
$lease.UpdateViewData('State')
}
$lease.UpdateViewData()
if($lease.State -eq [VMware.Vim.HttpNfcLeaseState]::ready){
Write-Host "Lease timeout $($lease.Info.LeaseTimeout)"
$bufferSize = 10KB
$Timeout = 300
$bytesWritten = 0
foreach($device in ($lease.Info.DeviceUrl | where{$_.Disk})){
Write-Host "Transferring $($device.Url.Split('/')[-1])"
$ovfFile = "$($ovfPath)\$($device.Url.Split('/')[-1].Replace('disk',$vmName))"
$url = $device.Url.Replace('*',$vm.VMHost.Name)
$webRequest = [System.Net.WebRequest]::Create($url)
$response = $webRequest.GetResponse()
$responseStream = $response.GetResponseStream()
$fileStream = [System.IO.File]::OpenWrite($ovfFile)
$chunk = New-Object byte[] $bufferSize
while (($bytesRead = $responseStream.Read($chunk,0,$bufferSize)))
{
$totalBytesRead += $bytesRead
$fileStream.Write($chunk,0,$bytesRead)
$fileStream.Flush()
}
$bytesWritten += $fileStream.Length
$ovfFileObj = New-Object VMware.Vim.OvfFile
$ovfFileObj.DeviceId = $device.Key
$ovfFileObj.Path = $device.Url.Split('/')[-1]
$ovfFileObj.Size = $fileStream.Length
$files += $ovfFileObj
$FileStream.Close()
$responseStream.Close()
$lease.HttpNfcLeaseProgress([int]($bytesWritten/($lease.Info.TotalDiskCapacityInKB * 1KB) *100))
Write-Host "Bytes written $($bytesWritten)"
}
$cdp = New-Object VMware.Vim.OvfCreateDescriptorParams
$cdp.Description = 'Test OVF Export'
$cdp.Name = "$($vmName)"
if($includeMac){
$cdp.ExportOption += 'mac'
}
if($includeUuid){
$cdp.ExportOption += 'uuid'
}
if($includeExtraConfig){
$cdp.ExportOption += 'extraconfig'
}
$cdp.OvfFiles = $files
$ovfResult = $ovfMgr.CreateDescriptor($vm.ExtensionData.MoRef,$cdp)
$xmlOvfDescriptor = ([xml]$ovfResult.OvfDescriptor)
$xmlOvfDescriptor.Envelope.References.File | %{
$_.href = $_.href.Replace('disk',$vmName)
}
$xmlOvfDescriptor.Save("$($ovfPath)\$($vmName).ovf")
$lease.HttpNfcLeaseProgress(100)
$lease.HttpNfcLeaseComplete()
Write-Host "Export complete"
}
else{
Write-Host "Export failed"
Write-Host $lease.Error[0].LocalizedMessage
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi,
In my customer environment i am getting below. How i can avoid this exception.
Exception calling "HttpNfcLeaseProgress" with "1" argument(s): "Operation timed out."
At C:\users\sbrady\Documents\Scripts\VMware\VMExport_v1.ps1:69 char:9
+ $lease.HttpNfcLeaseProgress([int]($bytesWritten/($lease.Info. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
Bytes written 2734714880
Exception calling "HttpNfcLeaseProgress" with "1" argument(s): "Operation timed out."
At C:\users\sbrady\Documents\Scripts\VMware\VMExport_v1.ps1:95 char:5
+ $lease.HttpNfcLeaseProgress(100)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
Exception calling "HttpNfcLeaseComplete" with "0" argument(s): "Operation timed out."
At C:\users\sbrady\Documents\Scripts\VMware\VMExport_v1.ps1:96 char:5
+ $lease.HttpNfcLeaseComplete()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
Regards,
Chethan Kumar
Can you check what the web timeout value is?
Do a Get-PowerCLIConfiguration, and check the value under WebOperationTimeoutSeconds.
And try increasing that value with the Set-PowerCLIConfiguartion cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference