VCD | Upload or Download an OVA\OVF file to and from Org vDC with ovftool

Hi Guys,

First of all I am sorry that I couldn't write up since last few days due to some personal work. But today I got some time and I thought to write about the subject.

I have seen many techy guys struggling with ovftools to upload the ova\ovf file into orgvDC. Generally, people love to upload ovf\ova file through GUI but sometime it just doesn't work due to x reason then you have this alternate way of uploading it. You can also use ovftool to see the exact error\progress while uploading it. Ovftool is helpful in many other operations but today's post is to learn that How to upload ova\ovf file through ovftool. right?

Now, as you know that I prefer to write some unique content or the content with enough elaboration that anyone can understand it. So, I saw a few article on this subject which I think will be little bit difficult for a lesser expert person to understand.

So, I will not only put the text here but also will create a clear video and will make sure that every info here will just work perfectly.

Below is the command -

To download

ovftool "vcloud://gjohar@vcd1.homelabs.com?org=vcnotes&catalog=Test&vappTemplate=Windows_2019" "C:\temp\Windows_2019.ova"

Explanation-

ovftool - It is the command string, you need to use
vcloud - It is to tell the ovftool that this operation is to be taken for vcloud director
gjohar - user account name created in org "vcnotes"
vcd1.homelabs.com - VCD URL to use with third party tools like ovftools
Test - It is the catalog name from where we are downloading the ova named Windows_2019
Windows_2019 - It is the vapp template or ova which is stored in Catalog named Test.

As per above command, we are downloading it to Temp directory. Also, just change the extension to Windows_2019.ovf, if you want to download ovf file. Got it?

To upload


ovftool "C:\temp\Windows_2019.ova" "vcloud://gjohar@vcd1.homelabs.com?org=vcnotes&catalog=Test&vappTemplate=Windows_2019"

it is just the reverse action. In above command, the ova file placed in C:\temp with given name will be uploaded into the catalog Test with name Windows_2019 in Org named "vcnotes" and with user account gjohar.

Hope it was easy.

I will buy sometime to create a video on it. Actually, I will be away from vCD terminal for next few days. In case, anyone still have some issues with this download and uploading ova/ovf file then you can share you vcd terminal and I will help you live ;)

In my next post, I will write about uploading and download ISO file. We will use ovftools only and it has little different command than uploading\download ova\ovf file.

Keep visiting the blog guys!


Thank you,
vCloudNotes





PS | Script to detect and delete the Orphaned Files

Hi Guys,

Someone recently asked about the script which can detect the orphaned files in a datastore and can remove it with full control and condition was, it should not show the files which are not modified since last x number of days rather it should show the files which are not connected with any VM.

I did little effort and could find that such task is already done brilliantly by someone on Internet.

What I tested -

I removed one vmdk file from VM and then ran the script it exactly shows the vmdk which I removed and then run the command to delete it but it tried to delete entire folder. Go through below line, it will be clear.

Below is the script-

#Start here
function Remove-OrphanedData {
[CmdletBinding(SupportsShouldProcess=$true)]
param(
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[PSObject[]]$Datastore,
[switch]$Delete
)
begin{
$fldList = @{}
$hdList = @{}
$fileMgr = Get-View FileManager
}
process{
foreach($ds in $Datastore){
if($ds.GetType().Name -eq "String"){
$ds = Get-Datastore -Name $ds
}
if($ds.Type -eq "VMFS" -and $ds.ExtensionData.Summary.MultipleHostAccess){
Get-VM -Datastore $ds | %{
$_.Extensiondata.LayoutEx.File | where{"diskDescriptor","diskExtent" -contains $_.Type} | %{
$fldList[$_.Name.Split('/')[0]] = $_.Name
$hdList[$_.Name] = $_.Name
}
}
Get-Template | where {$_.DatastoreIdList -contains $ds.Id} | %{
$_.Extensiondata.LayoutEx.File | where{"diskDescriptor","diskExtent" -contains $_.Type} | %{
$fldList[$_.Name.Split('/')[0]] = $_.Name
$hdList[$_.Name] = $_.Name
}
}
$dc = $ds.Datacenter.Extensiondata
$flags = New-Object VMware.Vim.FileQueryFlags
$flags.FileSize = $true
$flags.FileType = $true
$disk = New-Object VMware.Vim.VmDiskFileQuery
$disk.details = New-Object VMware.Vim.VmDiskFileQueryFlags
$disk.details.capacityKb = $true
$disk.details.diskExtents = $true
$disk.details.diskType = $true
$disk.details.thin = $true
$searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$searchSpec.details = $flags
$searchSpec.Query += $disk
$searchSpec.sortFoldersFirst = $true
$dsBrowser = Get-View $ds.ExtensionData.browser
$rootPath = "[" + $ds.Name + "]"
$searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
foreach($folder in $searchResult){
if($fldList.ContainsKey($folder.FolderPath.TrimEnd('/'))){
foreach ($file in $folder.File){
if(!$hdList.ContainsKey($folder.FolderPath + $file.Path)){
New-Object PSObject -Property @{
Folder = $folder.FolderPath
Name = $file.Path
Size = $file.FileSize
CapacityKB = $file.CapacityKb
Thin = $file.Thin
Extents = [string]::Join(',',($file.DiskExtents))
}
if($Delete){
If ($PSCmdlet.ShouldProcess(($folder.FolderPath + " " + $file.Path),"Remove VMDK")){
$dsBrowser.DeleteFile($folder.FolderPath + $file.Path)
}
}
}
}
}
elseif($folder.File | where {"cos.vmdk","esxconsole.vmdk" -notcontains $_.Path}){
$folder.File | %{
New-Object PSObject -Property @{
Folder = $folder.FolderPath
Name = $_.Path
Size = $_.FileSize
CapacityKB = $_.CapacityKB
Thin = $_.Thin
Extents = [String]::Join(',',($_.DiskExtents))
}
}
if($Delete){
if($folder.FolderPath -eq $rootPath){
$folder.File | %{
If ($PSCmdlet.ShouldProcess(($folder.FolderPath + " " + $_.Path),"Remove VMDK")){
$dsBrowser.DeleteFile($folder.FolderPath + $_.Path)
}
}
}
else{
If ($PSCmdlet.ShouldProcess($folder.FolderPath,"Remove Folder")){
$fileMgr.DeleteDatastoreFile($folder.FolderPath,$dc.MoRef)
}
}
}
}
}
}
}
}
}
Remove-OrphanedData -datastore Name_of_datastore


#It will give you below kind of output


Now, if you want to delete this then use below command

Remove-OrphanedData -datastore Name_of_datastore -delete

But before deleting it, let's see what it will be delete. See below pic. I have added a switch -whatif in last after -delete switch.


It will delete entire folder not only single file.

Now, It is up to you that whether you want to remove the entire folder by adding only -delete switch or if you want to delete only vmdk file not entire folder and that will be manual action.

In my case, deletion gave error while deleting the entire folder, might be due to in-use vmx file or so...

Any doubt, let's discuss.

That's it for now. Enjoy the learning!




Thank you,
vCloudNotes