Root ramdisk, scratch KB

scratch partition

Finding orphaned, disconnected, inaccessible, invalid vms using PowerCLI.

Today i want to show a way of finding vms that are orphaned, disconnected, inaccessible, invalid vms using PowerCLI.
Again , we will be utilizing get-view for that with proper filter.
If the VM gets into status of disconnected, orphaned, inaccessible or invalid it will change it’s connectionstate property. So , basics:
how can we know what kind of connectionstate are thee ?


This property is described on vsphere documentation website -> one one one.
Let’s find now the vms with oprhaned connectionstate.

get-view -ViewType VirtualMachine -Filter @{'RunTime.ConnectionState'='orphaned'}


So if you want to look for disconnected instead of orphaned vms, you just change the property ‘connectionstate’ value to ‘disconnected’, or you can just look for other than connected.

get-view -ViewType VirtualMachine -Filter @{'RunTime.ConnectionState'='disconnected|inaccessible|invalid|orphaned'} | select name

or the other way around which is , those which are not in the ‘connected’ state:

get-view -ViewType VirtualMachine -Filter @{'RunTime.ConnectionState'='^(?!connected).*$'}

Find vms with active vmware tools installation using PowerCLI.

Today i want to show you how to fix and find vms with active vmware tools installation (hanging/broken).
1) Locate all the vms with the issue
2) Fix // end vmware tools installation.

Step 2, should be normally easy. Just use option from VM context menu (Guest-> End VMware tools install). In case that it will not work you can follow the KB:
The KB says : “Ensure to not use SCSI eject”.
I had few vms that had this issue, the ‘End VMware tools install’ option went away actually after issuing:

eject -v --cdrom /dev/cdrom

Make sure you wait , 3-5 seconds after issuing this, and then check if the vmware tools install is done.
After issuing this command VM went back to normal state, and i could perform vmotions for those vms.

Step 1, for locating those vms with hanging vmware tools installation we can use get-view with proper filter

$affectedvms = get-view -viewtype virtualmachine -property 'name' -Filter @{'Runtime.ToolsInstallerMounted'='True'}

Ok, so i have 11 vms with mounted vmware tools installation iso. I will now unmount it in batch.

foreach ($vm in $affectedvms) {$vm.UnmountToolsInstaller()}

Ok, to be 100% sure, we have to check afterwards if everything went smooth or we hit the issues described in KB mentioned earlier.

$affectedvms = get-view -viewtype virtualmachine -property 'name' -Filter @{'Runtime.ToolsInstallerMounted'='True'}

It is now 2, so we have to manually now fixe 2 vms. Let’s do that now.

Let’s mount vmware tools iso for this VM that is in this case a linux vm.

After this mount the cd in /media for example, and type :
“eject -v –cdrom /dev/cdrom”
like on the screenshot

While running loops with UnmountToolsInstaller() method watch out for vms that will get into a ‘Question’.

PowerCLI C:\> $a |Get-VMQuestion

Text                                               Options              Default
----                                               -------              -------
msg.cdromdisconnect.locked:The guest operating     {Yes, No}            Yes
system has locked the CD-ROM door and is probably
using the CD-ROM, which can prevent the guest
from recognizing media changes. If possible,
eject the CD-ROM from inside the guest before

Disconnect anyway and override the lock?

Also i believe you need to have in your system ‘eject’ program. Even if i tried to reinstall vmware tools to get this fixed, it was unable to finish as it was reporting missing ‘eject’.
For windows boxes, like the KB says, shutdown the VM and check for connected isos.

This is also useful 😉
get-view -viewtype virtualmachine -property ‘name’ -Filter @{‘Runtime.ToolsInstallerMounted’=’True’} | %{Invoke-VMScript -VM $_.Name -ScriptText ‘eject -v –cdrom /dev/cdrom’ -ScriptType Bash -GuestCredential $cred}
It will try to pass the eject command to affected vms.

Using SearchIndex in PowerCLI , searching vms by their dns name, ip, and more

Today i want to bring closer something that might not be utilized that often.
What is SearchIndex and what can it do for us you might ask ? Well, best description is at the source 😉 so

The SearchIndex service allows a client to efficiently query the inventory for a specific managed entity by attributes such as UUID, IP address, DNS name, or datastore path. Such searches typically return a VirtualMachine or a HostSystem. While searching, only objects for which the user has sufficient privileges are considered. The findByInventoryPath and findChild operations only search on entities for which the user has view privileges; all other SearchIndex find operations only search virtual machines and hosts for which the user has read privileges. If the user does not have sufficient privileges for an object that matches the search criteria, that object is not returned.

After connecting to virtual center using PowerCLI, you want to initialize the SearchIndex first:

We start from getting ServiceInstance, from the content we take view of the SearchIndex

$si = get-view -id ServiceInstance
$SearchIndex = Get-view -id $si.Content.SearchIndex

What can do SearchIdex for us ?

$SearchIndex | gm -MemberType 'Method'

TypeName: VMware.Vim.SearchIndex

Name                Member
----                ------
Equals              Method
FindAllByDnsName    Method
FindAllByIp         Method
FindAllByUuid       Method
FindByDatastorePath Method
FindByDnsName       Method
FindByInventoryPath Method
FindByIp            Method
FindByUuid          Method
FindChild           Method

How to get a VM with dns name of X, well you can filter that vm from the all vms by looking at their properties for example. Another example is to use get-view with filter on that hostname property :

get-view -ViewType virtualmachine -filter @{'Guest.Hostname'='vm01.domain.corp'}

And now we are going to utilize SearchIndex to do the same:

Type		Value
----		-----
VirtualMachine	vm-8254

In return we have received ManagedObjectReference to a virtualmachine. If we want to see it , we would have to get its view.

get-view -id $SearchIndex.FindAllByDnsName($null,'vm98.domain.corp',$true)

The FindAllByDnsName has 3 arguments, the middle one is obviously the queried dns name. First one is for specifying the datacenter in which you want to look for it. The third, is for choosing the type, when it is set to $true, then we will be looking for a vm with that name, if $false, then we will be looking for a vmhost with that name.
For example if looking up host for specific name:

get-view -id $SearchIndex.FindAllByDnsName($null,'esxi01.domain.corp',$false)  

For the datacenter argument , what is expected is a ManagedObjectReference to a particular datacenter. First we need to get the moref for example of our datacenter:

(get-view -ViewType datacenter -Filter @{'name'='OurDC'}).MoRef

And use it with the SearchIndex FindAllByDnsName method

get-view -id $SearchIndex.FindAllByDnsName((get-view -ViewType datacenter -Filter @{'name'='OurDC'}).MoRef,'ourVM01.domain.corp',$true)

Let’s move forward,

We can use Get-View with proper filter like :

get-view -ViewType VirtualMachine -Filter @{'Guest.IpAddress'=''}

or utilize SearchIndex for that



VMware.Vim.ManagedObjectReference[] FindAllByIp(
VMware.Vim.ManagedObjectReference datacenter, 
string ip, 
bool vmSearch

First and thrid argument usage is the same as in previous exmaple.


VMware.Vim.ManagedObjectReference FindByUuid(
VMware.Vim.ManagedObjectReference datacenter, 
string uuid, 
bool vmSearch, 
System.Nullable[bool] instanceUuid

For vm searches we set the vmSearch to true, then if instanceUuid is set to true we can search for using its instanceUuid instead of bios uuid. for example:

-using VC InstanceUuid
-using SMbios Uuid



VMware.Vim.ManagedObjectReference FindByInventoryPath(string inventoryPath)





VMware.Vim.ManagedObjectReference FindByDatastorePath(VMware.Vim.ManagedObjectReference datacenter, 
string path


$SearchIndex.FindByDatastorePath((get-view -ViewType datacenter -Filter @{'name'='FirstFloorDatacenter'}).MoRef,'[DS09_FastDatastore] VMxyz/VMxyz.vmx')



VMware.Vim.ManagedObjectReference FindChild(
VMware.Vim.ManagedObjectReference entity, 
string name


$cluster = (Get-View -ViewType ClusterComputeResource -Filter @{'name'='ClusterA'}).moref
-finds hostA in ClusterA