forked from sijis/pyvmomi-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reboot-vm.py
40 lines (33 loc) · 1.21 KB
/
reboot-vm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from pyVmomi import vim
from pyVim.connect import SmartConnect, Disconnect
import vmutils
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--username',
help='Username of the vcenter user that you want to login with. ')
parser.add_argument('--password',
help='Password of the vcenter user that you want to login with. ')
parser.add_argument('--vcenter',
help='Domain of the vsphere client. i.e.: somevcenter.yourhost.com ')
parser.add_argument('--vcenterport',
help='Port number of the vsphere client. Example: 443 ')
parser.add_argument('--vmname',
help='Name of virtual machine as it appears in vsphere. i.e.: example-mysqldb-server ')
ARGS = parser.parse_args()
try:
si = SmartConnect(user=ARGS.username,
pwd=ARGS.password,
host=ARGS.vcenter,
port=ARGS.vcenterport)
except IOError, e:
pass
# Finding source VM
vm = vmutils.get_vm_by_name(si, ARGS.vmname)
# does the actual vm reboot
try:
vm.ResetGuest()
except:
# forceably shutoff/on
# need to do if vmware guest additions isn't running
vm.ResetVM_Task()
Disconnect(si)