Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 236824

Re: Get all VMs with "vmware-vim-cmd vmsvc/getallvms"

$
0
0

Okay, you can do it for VMs that have a name with "[" in it. But, it is ugly. The following command gives you vmid, vmname, and vmpath.

 

vim-cmd vmsvc/getallvms | awk '{ if ($1 == "Vmid") { fcol=((index($0, "Name") - 1)*2) + 1; flen=index($0, "Guest OS") - fcol } else { if (substr($0, fcol + 1,

1) == "[") fcol = fcol + 1; beg=substr($0, 1, fcol - 1); vmid=beg; sub(/[^0-9].*/, "", vmid); name=beg; sub(/[0-9]+\s+/,"",name); sub(/\s*$/, "", name); path=sub

str($0, fcol, flen); sub(/\s+\w*$/, "", path); if (vmid != "") print vmid "\" name "\" path } }

 

In the awk script:

  1. Look in the header for the "Name" text
  2. Use the magic formula [ file column = 2("Name" column - 1) + 1 ] (found through educated guessing about how I would write the header)
  3. Get a representative length of the file path by using the index of the "Guest OS" header (this length runs into the middle of the Guest OS but that gets stripped out later.
  4. If it isn't the header, first check to see if the magic formula is off by one and if it is, fix it
  5. Using the file column value, fcol, get the beginning of the getallvms line that contains the vmid and the vm name
  6. Parse that beginning part into the vmid and the name.
  7. Parse the path out of substr($0, fcol,flen) by stripping off the remaining piece of the guest OS and the trailing whitespace.

Note that the VM name is not necessarily unique - the same name can be reused in a different resource pool.

 

I used backslashes to separate the fields because they get translated into %5C in the VM name.


Viewing all articles
Browse latest Browse all 236824

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>