-
Notifications
You must be signed in to change notification settings - Fork 6
/
hyperv.py
57 lines (55 loc) · 1.83 KB
/
hyperv.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
46
47
48
49
50
51
52
53
54
55
56
57
# note: this file needs to be run as admin for commands to work
import subprocess
from pathlib import Path
from sys import platform
class hyperv(object):
runningVMs = None
VMs = None
def updateVMs(self):
VMs = subprocess.run(["powershell", "Get-VM | Select Name"], stdout=subprocess.PIPE)
VMs = VMs.stdout.decode("utf-8")
VMs = VMs.split("\r\n")
self.VMs = [x for x in VMs if len(x)]
def updateRunningVMs(self):
runningVMs = subprocess.run(["powershell", "Get-VM | Where { $_.State -eq 'Running' } | Select Name"], stdout=subprocess.PIPE)
runningVMs = runningVMs.stdout.decode("utf-8")
runningVMs = runningVMs.split("\r\n")
self.runningVMs = [x for x in runningVMs if len(x)]
def updateOutput(self):
self.updateRunningVMs()
self.updateVMs()
def isFound(self):
if platform.lower() != "win32":
return False
elif self.runningVMs != []:
self.updateRunningVMs();
if "not recognized" in self.runningVMs[0]:
return False
else:
return True
else:
return True
def runCount(self):
if self.runningVMs == []:
return 0
else:
return len(self.runningVMs[2:])
def isRunning(self):
if self.runCount() > 0:
return True
else:
return False
def getGuestName(self, index = None):
if self.VMs == []:
return None
elif index != None:
return self.VMs[index + 2]
else:
return self.VMs[2:]
def getRunningGuestName(self, index = None):
if self.isRunning() == False:
return None
elif index != None:
return self.runningVMs[index + 2]
else:
return self.runningVMs[2:]