-
Notifications
You must be signed in to change notification settings - Fork 0
/
vms_created_last24hours.py
45 lines (38 loc) · 1.43 KB
/
vms_created_last24hours.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
41
42
43
44
45
import atexit
from pyVim.connect import SmartConnectNoSSL, Disconnect
from pyVmomi import vim
from tools import cli
from datetime import datetime, timedelta
def setup_args():
"""
Get standard connection arguments
"""
parser = cli.build_arg_parser()
my_args = parser.parse_args()
return cli.prompt_for_password(my_args)
def main():
args = setup_args()
si = None
try:
si = SmartConnectNoSSL(host=args.host,
user=args.user,
pwd=args.password,
port=int(args.port))
atexit.register(Disconnect, si)
except vim.fault.InvalidLogin:
raise SystemExit("Unable to connect to host "
"with supplied credentials.")
content = si.RetrieveContent()
for child in content.rootFolder.childEntity:
if hasattr(child, 'vmFolder'):
datacenter = child
vmfolder = datacenter.vmFolder
vmlist = vmfolder.childEntity
diff_24hour = datetime.now() - timedelta(hours = 24)
for vm in vmlist:
# Assuming content.rootFolder.childEntity.vmlist[..].creationTime is VM created time
if datetime.strptime(vm.creation.Date, '%b %d %Y %I:%M%p') < diff_24hour:
print("VM Name: " + vm.name + "Creation Date: " + str(vm.creation.Date))
# Start program
if __name__ == "__main__":
main()