-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVDI_By_Day_Compute_By_Night_v0.1.ps1
160 lines (137 loc) · 7.1 KB
/
VDI_By_Day_Compute_By_Night_v0.1.ps1
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
###
# VDI by day Compute by Night
# version 0.2
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
###
#Assumes you have already connected to the vCenter. If adjust the following line and uncomment.
#Connect-VIServer -server vsa01.YourDomain.local -user administrator@vSphere.local -password Passw0rd!
#UPDATE WITH CORRECT PATH
#Import vGPU capacity function
. 'C:\Users\UserName\Desktop\vGPUcapacity.ps1'
#define the paramaters
# VDI Side
$SpareVMcapacity = 1 #How many spare VMs should be able to be powered on
# Compute Side
$ComputeVMbaseName = "Compute" #The base name of compute VMs, a three digit number will be added at the end
$ComputeCountFormat = "000" #The preceding zeros in the compute name, so the 6th VM would be Compute006
$StartingVMnum = 1 #The starting VM number, for example 001 would be the first VM monitored
$MaxComputeVMs = 4 #Total Number of Compute VMs in use
$ComputevGPUtype = "grid_p4-2q" #Which vGPU is in the Compute VM (later I will detect this)
# Opperations Side Varables
$WorkingCluster = "Horizon" #Name of the cluster that should be
$SecondsBetweenScans = 10 #How long will the program wait between scans
$NumberOfScansToPreform = 10 #How many times should the scan be run
###############################
###############################
# Operational variables do not touch
$vGPUslotsOpen = 0 #How many vGPU VMs are currently on
$POComputeVMcount = 0 + $StartingVMnum #Number of powered on (PO) compute VMs
$ScanCount = 0 #How Many times the scan has been run
$CurrVMName = "" #Current VM Name
$ComputeSteadyState = 0
#works on a Last In First Out (LIFO) process
While($ScanCount -le $NumberOfScansToPreform)
{
#see what we already have running in the environment so we can start at the right spot.
#assumes that VMs are on or off in order IE 1,2,3 are all powered on NOT 1,3,4 on
$QuickCount = 0 + $StartingVMnum
$POComputeVMcount = 0 + $StartingVMnum
while($QuickCount -lt ($MaxComputeVMs + $StartingVMnum))
{
$CurrVMName = $ComputeVMbaseName + $QuickCount.ToString($ComputeCountFormat)
write-Host "Checking VM: " $CurrVMName
$ComputeName = Get-VM $CurrVMName
#write-Host "VM power state: " $ComputeName.powerState
if($ComputeName.powerState -eq "PoweredOn"){
$POComputeVMcount++
#write-Host "VM already running: " $CurrVMName
}
write-Host "VM " $CurrVMName " power state: " $ComputeName.powerState
$QuickCount++
}
while($ComputeSteadyState -eq 0) #Equlibrium while, it will keep going till the system is in the steady state desired
{
$vGPUslotsOpen = vGPUSystemCapacity $ComputevGPUtype $WorkingCluster "connected" #Make sure there is room to prepare the compute VMs
Write-Host "vGPU Slots open: " $vGPUslotsOpen
#Eventually add statement of hosts capacity
write-Host "___________________________________________________________"
write-Host "vGPU Slots Open: " $vGPUslotsOpen " Spare Capacity: " $SpareVMcapacity
write-Host "Compute Count: " $POComputeVMcount " Max VMs: " $MaxComputeVMs " Starting VMs: " $StartingVMnum
write-Host "___________________________________________________________"
if($vGPUslotsOpen -lt $SpareVMcapacity -and $POComputeVMcount -ge (0 + $StartingVMnum)) #suspend to keep capacity till we can't power off any more
{
write-Host "-----------------------------------------------------------"
write-Host "Decreasing running workload VMs"
write-Host "vGPU slots open: " $vGPUslotsOpen " Required Spare Capacity: " $SpareVMcapacity
# suspend if the capacity is not sufficent
$POComputeVMcount-- #decrease powered on VMs by 1
$CurrVMName = $ComputeVMbaseName + $POComputeVMcount.ToString($ComputeCountFormat)
#$vGPUslotsOpen = vGPUSystemCapacity $ComputevGPUtype $WorkingCluster "connected" #Make sure there is room to prepare the compute VMs
$ComputeName = Get-VM $CurrVMName
if($ComputeName.powerState -eq "PoweredOn"){
Suspend-VMGuest $ComputeName
Start-Sleep -s 5
write-Host "Suspend VM: " $ComputeName
}
else {write-Host "VM " $ComputeName " state: " $ComputeName.powerState}
write-Host "-----------------------------------------------------------"
}
elseif($vGPUslotsOpen -gt $SpareVMcapacity -and $POComputeVMcount -lt ($MaxComputeVMs + $StartingVMnum)) #Resume/power on till we reach the maximum number of VMs
{
write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
write-Host "Increaseing running workload VMs"
write-Host "vGPU slots open: " $vGPUslotsOpen " Required Spare Capacity: " $SpareVMcapacity
# start or resume next Compute VM
$CurrVMName = $ComputeVMbaseName + $POComputeVMcount.ToString($ComputeCountFormat)
$ComputeName = Get-VM $CurrVMName
if ($ComputeName.powerState -eq "PoweredOff" -or $ComputeName.powerState -eq "Suspended"){
write-Host "Changing " $ComputeName " from " $ComputeName.powerState " to PoweredOn"
Start-VM $ComputeName
write-Host "Increased running workload VM with: " $ComputeName
}
else {write-Host "VM already started: " $ComputeName}
$POComputeVMcount++ #increase powered on VMs by 1 placed here so it moves passed VMs already powered on
#$vGPUslotsOpen = vGPUSystemCapacity $ComputevGPUtype $WorkingCluster "connected" #Make sure there is room to prepare the compute VMs
write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
}
else
{
#Cant do anything break the loop
write-Host "==========================================================="
$ComputeSteadyState = 1
write-host "Reached Steady State"
write-Host "==========================================================="
}
}
$ComputeSteadyState = 0
write-Host ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
write-Host "Starting Sleep for: " $SecondsBetweenScans " Seconds"
Start-Sleep -s $SecondsBetweenScans #sleep for amount of time then resume
Write-Host "Finished sleep"
Write-Host "Scan Count: " $ScanCount
$ScanCount++
}
# clean up and suspend compute VMs
write-Host "***********************************************************"
write-Host "Starting clean up..." $POComputeVMcount
while($POComputeVMcount -gt (0 + $StartingVMnum))
{
$POComputeVMcount-- #decrease powered on VMs by 1
# suspend if the capacity is not sufficent
write-Host "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
$CurrVMName = $ComputeVMbaseName + $POComputeVMcount.ToString($ComputeCountFormat)
write-Host "Cleaning up: " $CurrVMName
$ComputeName = Get-VM $CurrVMName
if($ComputeName.powerState -eq "PoweredOn"){
write-Host "Starting suspend of " $ComputeName
Suspend-VMGuest $ComputeName
Start-Sleep -s 5
write-Host "Suspend VM: " $ComputeName
}
else {write-Host "VM already stopped: " $ComputeName}
write-Host "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
}
write-Host "***********************************************************"
Write-Host "Script finished, Good-bye"