This assumes a single datacenter object, which is fine when connecting directly to an ESXi host, but breaks if you're going through a vCenter that has more than one datacenter defined.