There was an interesting article this morning by my colleague Dave Hill about Looking up Managed Object Reference (MoRef) in vCenter Server, which references a VMware KB article showing you how to use vSphere MOB to find MoRef IDs.
A Managed Object Reference ID also known just as MoRef ID is a unique value that is generated by the vCenter Server and is guaranteed to be unique for a given entity in a single vCenter instance. Steve Jin has a great article going into detail about MoRef's, that you should check out here. MoRef ID's are not only used in vSphere, but they can also be referenced in other VMware products such as vCD (vCloud Director) or SRM to name a few or even by 3rd party/custom tools as a way to uniquely track objects within vCenter.
Using the vSphere MOB is one way of retrieving the MoRef ID, but of course this can be tedious if you are trying to locate MoRef's for multiple entities, in multiple vCenters. I decided to write a quick vSphere SDK for Perl script called moRefFinder.pl that allows a users to quickly query for a variety of Managed Objects (VM,Host,Cluster,Datacenter,Resource Pool,Network, Distributed vSwitch, Folder, vApp and Datastore) in a vCenter Server.
I also noted earlier, MoRef ID is only unique within a vCenter instance, so how do you track these objects across multiple vCenters? Well, VMware introduced a new property called instanceUUID (128 bit UUID) which is unique for a given vCenter Server and you can use this along with the MoRef ID to uniquely track objects across multiple vCenters and the script automatically outputs this value for any query.
Note: There have been some optimization in the latest vSphere SDK for Perl 5.0, it is recommended you use the latest version which is backwards compatible with vSphere 3.x and 4.x
The script requires two parameters:
- type - The type of managed object
- name - The name of the object as seen in vCenter Server
Here is an example querying for a Virtual Machine:
Here is an example querying for a Host:
Here is an example querying for a Cluster:
Here is an example querying for a Datacenter:
Here is an example querying for a Resource Pool:
Here is an example querying for a Network (Portgroup):
Here is an example querying for a Distributed vSwitch:
Here is an example querying for a Folder:
Here is an example querying for a vApp:
Here is an example querying for a Datastore:
Michael says
Nice work William, is the 5.1 version of vSphere SDK for Perl supported?
I keep getting the below error after it reports the vCenterInstanceUUID:
Use of uninitialized value in concatenation (.) or string at C:\moRefFinder.pl line 88.
Can't call method "value" on an undefined value at c:\moRefFinder.pl line 88.
William says
Yes this works on 5.1 as well, make sure you're typing in the correct entity name (display name) and type, else it will not be able to find the object and hence it would print undefined value.
Michael says
Brilliant, thanks! working great now.
forgot that when in doubt, check for case sensitive.
Anonymous says
how to retrive the uuid of the resourcepool from the data center
??? using vi java api...plz help me
Javier Viola says
Hi William, great work! I added support to get the MoRef ID of datastore clusters... this is the diff
40c40
< help => "vm|host|cluster|datacenter|rp|network|dvs|folder|vapp|datastore",
---
> help => "vm|host|cluster|datacenter|rp|network|dvs|folder|vapp|datastore|dsCluster",
68c68,69
< 'datastore' => 'Datastore'
---
> 'datastore' => 'Datastore',
> 'dsCluster' => 'StoragePod'
regards
Michał Czerwiński says
Hello.
Your Perl script is very helpfull but could You provide also Python version?
Thanks.
Jim says
Thanks for the great write up William! Do you know if the MoRef ID is guaranteed to be *historically* unique in a single vSphere server? If a VM is deleted will another VM eventually re-use the old MoRef ID? I have seen a lot of assumptions in blogs/forums, and hoped you might have a definitive answer.
lamw says
That's correct, a MoRef ID is guaranteed to be unique AND it can potentially re-use an old MoRef that has been recycled. I've personally seen this in environment's I've managed where the churn of VMs are quite high such as Continuous Integration/Deployments.
Jim says
Thank you for the quick reply!
If we are going to have to resort to a UUID as a historically unique reference, is there a way to include the UUID in events from an event collector so that we do not need to store a MoRef ID to UUID mapping or query vSphere for the UUID based on the MoRef ID in the event?
William Lam says
Yes, it's just like any other property. In the property collector spec, you specify what you're interested in. MoRef ID will always be there given it's the unique identifier and you can specify the instanceUUID if you want to use that in conjunction. If you're dealing with more than 1 VC, I would recommend giving up on the MoRef ID and concatenating VC InstanceUUID + VM InstanceUUID.
Take a look at these two blog posts http://blogs.vmware.com/vsphere/2012/02/uniquely-identifying-virtual-machines-in-vsphere-and-vcloud-part-1-overview.html & http://blogs.vmware.com/vsphere/2012/02/uniquely-identifying-virtual-machines-in-vsphere-and-vcloud-part-2-technical.html which provides more details regarding this topic
Jim says
Thank you once again! If I am going way off topic, I apologize. A response of "Look elsewhere" is totally acceptable. 🙂
I am waiting for updates on latestPage of an EventHistoryCollector for a set of VM events under the root folder, but cannot see how to include the VM InstanceUUID of each event in those updates. If you know of an example, I'd appreciate a redirect. If not, I'll continue my search.
hotspicyramen says
You can use vim-cmd vmsvc/getallvms to get vMid ? is it the same?
William Lam says
Yes, you can it connecting directly to ESXi host IF you plan to work with it directly. If you have vCenter Server, then it'll have a diff MoRef ID and you'll need to use the API
Bhavya Shah says
Hello, what if I want the ManagedObjectReference of vCenter server itself.?
Actually my ultimate goal is to get vcDebugInfo performance metrics using vSphere API, which wants us to pass moid for method queryperf, but as per my understanding these metrics belong to vCenter, then what should I do?
Please help,
Thanks.