VMware {code} Community
arthurdent78
Contributor
Contributor
Jump to solution

list VMs in datastore but not registered in inventory

I have a perl script that can list all vms in a given datasore using the HostDatastoreBrowser object.

however this only pulls back vms that are registered. I want to be able to find images that users have 'removed from the inventory' but not destroyed on the datastore.

Can i even achive this by using the HostDatastorBrowser? If not how can I achive my goal?

Thanks,

0 Kudos
1 Solution

Accepted Solutions
Texiwill
Leadership
Leadership
Jump to solution

Hello,

You can but you may have to put the data in a different format. This is actually more a perl issue than a VI Toolkit Issue.

You would need to create the appropriate list then use the PERL map function to evaluate an expr over the entire list. Personally I would just use a foreach statement as well.


Best regards,
Edward L. Haletky
VMware Communities User Moderator
====
Author of the book 'VMWare ESX Server in the Enterprise: Planning and Securing Virtualization Servers', Copyright 2008 Pearson Education.
Blue Gears and SearchVMware Pro Blogs
Top Virtualization Security Links

--
Edward L. Haletky
vExpert XIV: 2009-2023,
VMTN Community Moderator
vSphere Upgrade Saga: https://www.astroarch.com/blogs
GitHub Repo: https://github.com/Texiwill

View solution in original post

0 Kudos
4 Replies
Steve_Jin
Expert
Expert
Jump to solution

I think you got the right managed object which should get you VMs not registered. Do you want to share you code?

Steve JIN, VMware Engineering

Creator of VI Java API: http://vijava.sf.net

Steve JIN Author of VMware VI and vSphere SDK; Creator of open source VI Java API (http://vijava.sf.net); Blogger at http://www.doublecloud.org
admin
Immortal
Immortal
Jump to solution

You can achieve this using HostDatastorBrowser but to filter the unregistered Virtual Machines you have to compare the vmx files returned with the vmx files of Virtual Machines available in Datastore.

Please refer below code snippet

my $host_data_browser = Vim::get_view(mo_ref => $datastore->browser);

my $datastore_mor = $host_data_browser;

my $ds_mor = $datastore;

my $path = '';

my $level = 0;

my $browse_task;

my $vm_array = Vim::get_views (mo_ref_array => $ds_mor->vm) ;

my @vms;

my $match = ;

foreach my $vm (@$vm_array){

push @vms, $vm->config->files->vmPathName;

}

my $files = FileQueryFlags->new(fileSize => 1,

fileType => 1,

modification => 1

);

my $hostdb_search_spec = HostDatastoreBrowserSearchSpec->new(

matchPattern => $match,

details => $files);

eval {

$browse_task = $datastore_mor->SearchDatastoreSubFolders(datastorePath=>$path,

searchSpec=>$hostdb_search_spec

);

};

if ($@)

elsif (ref($@->detail) eq 'InvalidDatastore')

else

}

else

}

my $vmxpath;

foreach (@$browse_task) {

my $found = 0;

if(defined $_->file) {

my $y = $_->folderPath;

foreach my $x (@{$_->file}) {

foreach my $vmx (@vms){

$vmxpath = $x->path;

my $path = $y."/".$x->path;

if($path eq $vmx){

$found = 1;

}

}

}

if( $found eq 0 && $vmxpath){

print "\n".$vmxpath." not registered.";

}

}

}

arthurdent78
Contributor
Contributor
Jump to solution

I have managed to get the code snippet to work up to a point.

I have changed the $match variable to and changed the $path to a lun.

However the probelm I have is that in the below part of the code posted, comparing $path and $vmx is never matching the correct names. (i confirmed this by printing each one to the screen and seeing the diffrenent VM names). This results in it thinking all VMX files are 'not registered'.

if($path eq $vmx){

$found = 1;

if( $found eq 0 && $vmxpath){

print "\n".$vmxpath." not registered.";

im confident that I am now seeing all vmx files in the LUN it is struggling however to compare them. is there a way of sorting each list into order. or perhaps take one row out of $path and check it against every line in the $vmx variable?

0 Kudos
Texiwill
Leadership
Leadership
Jump to solution

Hello,

You can but you may have to put the data in a different format. This is actually more a perl issue than a VI Toolkit Issue.

You would need to create the appropriate list then use the PERL map function to evaluate an expr over the entire list. Personally I would just use a foreach statement as well.


Best regards,
Edward L. Haletky
VMware Communities User Moderator
====
Author of the book 'VMWare ESX Server in the Enterprise: Planning and Securing Virtualization Servers', Copyright 2008 Pearson Education.
Blue Gears and SearchVMware Pro Blogs
Top Virtualization Security Links

--
Edward L. Haletky
vExpert XIV: 2009-2023,
VMTN Community Moderator
vSphere Upgrade Saga: https://www.astroarch.com/blogs
GitHub Repo: https://github.com/Texiwill
0 Kudos