VMware Cloud Community
Sany_1973
Enthusiast
Enthusiast
Jump to solution

Get the registered vCenter info from the datastore VM folder files

Hi Team,

I have VMs from multiple vcenters in a datastore , because the datastore is connected with multiple vCenters.

Is there any script available to get the registered vcenter for each VM from the VM folder files (vmdk,vmx,etc..)

How to get the registered vCenter from VM files in datastore.

Thanks,

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Like I already said in an earlier reply, you can't find the vCenter from the files you mention.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

You can get the registered vCenter from the VM like this.
Not sure why you want to use the datastore files for this.

Get-VM |

Select Name,@{N='vCenter';E={[System.Uri]$_.GetClient().Config.Server}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Sany_1973
Enthusiast
Enthusiast
Jump to solution

Thanks Luc.

This is a test lab and we have so many vcenters. If I can check the VM files in the datastore and find out in which vcneter its registered, we can directly got that vcenter.

Moreover some of the VMs are not registered with any vcneter.

Just want to know if there is any mark on vm files(vmx, vmdk...)regarding its registed vCenter.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, I see what you mean.

You need to be connected to all vCenters when running this script.

Give it a try

$flags = New-Object VMware.Vim.FileQueryFlags

$qConfig = New-Object VMware.Vim.VmConfigFileQuery

$qConfig.Details = New-Object VMware.Vim.VmConfigFileQueryFlags

$searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec

$searchSpec.details = $flags

$searchSpec.Query = $qFloppy,$qFolder,$qISO,$qConfig,$qTemplate,$qDisk,$qLog,$qRAM,$qSnap

$searchSpec.sortFoldersFirst = $true

foreach($ds in Get-Datastore){

    # Get datastore files

    $dsBrowser = Get-View -Id $ds.ExtensionData.browser

    $rootPath = "[" + $ds.Name + "]"

    $searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec) | Sort-Object -Property {$_.FolderPath.Length}

    foreach($folder in $searchResult){

        foreach ($file in $folder.File){

            $vmName = $file.Path.Split('.')[0]

            $vm = Get-VM -Name $vmName -ErrorAction SilentlyContinue

            New-Object PSObject -Property @{

                Datastore = $ds.Name

                VM = $vmName

                vCenter = &{

                    if($vm){

                        [System.Uri]$vm.GetClient().Config.Server

                    }

                    else{

                        'unregistered'

                    }

                }

            }

        }

    }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Sany_1973
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

Thank you very much for the help.Actually I am looking for a script with which I will get the registered vcenter for a VM, where the input is the folder name of the VM from the datastore.

Datastore Name is : datastore1

VM folder name : datastore1\VM1 ( contains vmx, vmdk......files)

searching inside these files, will it be able to get the registered vcenter for this VM

Thanks,

Stanly.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Like I already said in an earlier reply, you can't find the vCenter from the files you mention.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Sany_1973
Enthusiast
Enthusiast
Jump to solution

Thank you Luc.

Thanks for the help

Reply
0 Kudos