Skip to content

Commit

Permalink
This new sample file prints a list of VMs according to a provided str…
Browse files Browse the repository at this point in the history
…ing to match (vmware#594)

* This new sample file prints a list of VMs according to a provided string
to match.

* Pass pep8 check.

* Added attribute "-f" to "getallvms.py". This will allow to filter
all vCenter list according to a pattern string (case insensitive).
  • Loading branch information
rjrpaz authored and pgbidkar committed Aug 1, 2019
1 parent 57c82b5 commit fa32003
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions samples/getallvms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Python program for listing the vms on an ESX / vCenter host
"""

import re
import atexit

from pyVim import connect
Expand All @@ -27,6 +28,17 @@
import tools.cli as cli


def get_args():
parser = cli.build_arg_parser()
parser.add_argument('-f', '--find',
required=False,
action='store',
help='String to match VM names')
args = parser.parse_args()

return cli.prompt_for_password(args)


def print_vm_info(virtual_machine):
"""
Print information for a particular virtual machine or recurse into a
Expand Down Expand Up @@ -64,7 +76,7 @@ def main():
Simple command-line program for listing the virtual machines on a system.
"""

args = cli.get_args()
args = get_args()

try:
if args.disable_ssl_verification:
Expand All @@ -89,15 +101,22 @@ def main():
container, viewType, recursive)

children = containerView.view
if args.find is not None:
pat = re.compile(args.find, re.IGNORECASE)
for child in children:
print_vm_info(child)
if args.find is None:
print_vm_info(child)
else:
if pat.search(child.summary.config.name) is not None:
print_vm_info(child)

except vmodl.MethodFault as error:
print("Caught vmodl fault : " + error.msg)
return -1

return 0


# Start program
if __name__ == "__main__":
main()

0 comments on commit fa32003

Please sign in to comment.