1

What is the canonical way to use VMware's ovftool to query to see if a VM exists or not?

If I run this command:

ovftool vi://username@password@myvsphere/mydatacenter/vm/myvm

I might get this response:

Error: Got fault from server: The attempted operation cannot be
performed in the current state (Powered on).
Error: Fault cause: vim.fault.InvalidState

I know I can do the following and then look for "Locator does not refer to an object" but is there a different command or (set of) option(s) I can run that will return a simple true or false or something else concise that does not indicate an error?

ovftool vi://username@password@myvsphere/mydatacenter/vm/avmthatisnothere

Just FYI the above returns this:

Error: Locator does not refer to an object:
vi://username@password@myvsphere/mydatacenter/vm/avmthatisnothere
MikeSchinkel
  • 113
  • 7
  • A follow up: I ended up using `govc`. It requires vCenter's API, but fortunately for my use-case we were using vCenter: https://github.com/mikeschinkel/govmomi/blob/master/govc/USAGE.md – MikeSchinkel Aug 22 '22 at 17:25

2 Answers2

1

You cannot request operations on the vm while its running, but your output also suggests, that the VM you query actually exists. So if you get an output similar to:

Error: Got fault from server: The attempted operation cannot be performed in the current state (Powered on).
Error: Fault cause: vim.fault.InvalidState

The VM actually exists and is in ON state.

If you get something like

Error: Locator does not refer to an object: vi://...

The object / the VM does not exist.

Im unable to test the situation when the VM exists, but is turned off, but I believe you will get the information about the datasotore on which the VM is hosted etc.

Unfortunately there is no simple true/false command here. You can wrap this in a script, but you will have to parse the output of the command (stdout and stderr) to do the decision making.

MarcelH
  • 11
  • 2
0

You're using the wrong tool. You cannot serve beer with a fork.

VMware OVF Tool is a command-line utility that allows you to import and export OVF packages to and from many VMware products.

To check whether a VM with the name myVM exists you should try PowerCLI:

Get-VM -Name myVM

https://developer.vmware.com/docs/powercli/latest/vmware.vimautomation.core/commands/get-vm/#Default

Zac67
  • 10,320
  • 2
  • 12
  • 32
  • Seems strange that a tool that can power-on and power-off a VM is not the right tool to determine if a VMs actually exist to power on or off. That said, it seems like [a huge amount of effort](https://virtualizationreview.com/articles/2017/06/01/how-to-install-and-use-powershell-and-powercli-on-linux.aspx) to use the _"right"_ tool just to test for existence when by side-effect I can use `ovftool` in a much lighter weight manner to check the same thing. – MikeSchinkel Oct 09 '21 at 07:22