Skip to content

Instantly share code, notes, and snippets.

@pathcl
Created August 7, 2015 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pathcl/1552535ffb55931894cd to your computer and use it in GitHub Desktop.
Save pathcl/1552535ffb55931894cd to your computer and use it in GitHub Desktop.
Power on machines by uuid on VMware
#!/usr/bin/env python
# PowerOn machines based on uuid
from pyVim import connect
from tools import cli
from tools import tasks
from pyVmomi import vim
import ssl
import atexit
ssl._create_default_https_context = ssl._create_unverified_context
def setup_args():
parser = cli.build_arg_parser()
# TODO
#parser.add_argument('-j', '--uuid',
# help='file of uuid',
# required=False)
my_args = parser.parse_args()
return cli.prompt_for_password(my_args)
ARGS = setup_args()
try:
SI = connect.SmartConnect(host=ARGS.host,
user=ARGS.user,
pwd=ARGS.password,
port=ARGS.port)
atexit.register(connect.Disconnect, SI)
except IOError:
pass
with open('uuid-to-poweron.txt') as uuid:
for line in uuid:
line = line.strip()
if uuid:
try:
VM = SI.content.searchIndex.FindByUuid(None, line, True, True)
print("The current powerState is: {0}".format(VM.runtime.powerState))
TASK = VM.PowerOn()
tasks.wait_for_tasks(SI, [TASK])
print("")
print("The current powerState is: {0}".format(VM.runtime.powerState))
print("")
print("its done")
except vim.fault.InvalidPowerState:
print("")
print("{0} Its already powered On!!".format(line))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment