-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbalance-pools.ps1
222 lines (175 loc) · 10.5 KB
/
balance-pools.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
## Specify TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
## Specify your vCenter here.
Connect-VIServer vCenterServer.your.local
$Clusters = get-Cluster
Function Select-size {
## Separator
write-host "`n======================================"
## Display the cluster / vApp being worked on
if ($cluster) {Write-host "Cluster Name : " -nonewline; Write-Host $Cluster -f Green}
Write-Host "Resouce Pool / vApp: " -nonewline; Write-Host $ResourcePool -f Green
$script:CPU_shares = $null
$script:RAM_shares = $null
## Instead of asking for shares values, just ask if it should be high, medium or low.
write-host -f Cyan "`nPlease classify this system."
$Selection = Read-Host "
H: High CPU/RAM shares`
M: Medium CPU/RAM shares`
L: Low CPU/RAM shares `
--------------------------------------`
A: mAin menu`
Q: Quit`
Please make a selection H/M/L/A/Q"
Switch ($Selection)
{
'H' {$script:CPU_shares = 2000
$script:RAM_shares = 20}
'M' {$script:CPU_shares = 1000
$script:RAM_shares = 10}
'L' {$script:CPU_shares = 500
$script:RAM_shares = 5}
'A' {main-menu}
'Q' {exit}
default {Select-size}
}
write-host `n
}
Function Display-CPU-Result {
If ($PoolCPUdiff -eq "0") {
write-host "CPU shares currently: " -NoNewline; write-host -f Green $resourceCurrentCPUShares
write-host "CPU shares should be: " -NoNewline; write-host -f Cyan $ResourceCPU_shares_new
}
Else { ## Instead of displaying the information, offer to input new values
write-host "======================================"
write-host -f cyan "Please make a selection."
write-host `n
write-host "CPU shares currently: " -NoNewline; write-host -f Yellow $resourceCurrentCPUShares
write-host "CPU shares should be: " -NoNewline; write-host -f Cyan $ResourceCPU_shares_new
$update = read-host "
U: Update CPU shares from $resourceCurrentCPUShares to $ResourceCPU_shares_new`
N: No changes`
Update the shares? U/N"
Switch ($update)
{
'U' { if ($ResourcePool.ExtensionData -like ('*VirtualApp*')) {Set-vapp -vapp $resourcepool -NumCpuShares $ResourceCPU_shares_new}
elseif ($ResourcePool.ExtensionData -like ('*ResourcePool*')) {Set-resourcepool -ResourcePool $resourcepool -NumCpuShares $ResourceCPU_shares_new}
else {Write-Host -f Red "error, no action taken"}
}
'N' {}
default {Display-CPU-Result}
}
}
write-host ""
}
Function Display-RAM-Result {
If ($PoolRAMdiff -eq "0") {
write-host "RAM shares currently: " -NoNewline; write-host -f Green $RresourceCurrentRAMShares
write-host "RAM shares should be: " -NoNewline; write-host -f Cyan $ResourceRAM_shares_new
}
Else { ## Instead of displaying the information, offer to input new values
write-host "RAM shares currently: " -NoNewline; write-host -f Yellow $RresourceCurrentRAMShares
write-host "RAM shares should be: " -NoNewline; write-host -f Cyan $ResourceRAM_shares_new
$update = read-host "
U: Update RAM shares from $RresourceCurrentRAMShares to $ResourceRAM_shares_new`
N: No changespe`
Update the shares? U/N"
Switch ($update)
{
'U' { if ($ResourcePool.ExtensionData -like ('*VirtualApp*')) {Set-vapp -vapp $resourcepool -NumMemShares $ResourceRAM_shares_new}
elseif ($ResourcePool.ExtensionData -like ('*ResourcePool*')) {Set-resourcepool -ResourcePool $resourcepool -NumMemShares $ResourceRAM_shares_new}
else {Write-Host -f Red "error, no action taken"}
}
'N' {}
default {Display-RAM-Result}
}
}
write-host ""
}
Function Update-all-pools {
foreach ($Cluster in $Clusters)
{
## Get the resource pools for this cluster, omit the hidden pool named Resources
$Resources = $Cluster | Get-vApp
$Resources += $Cluster | Get-resourcepool | where-object {$_.name -notlike "*Resources*"}
if ($Resources) {
foreach ($ResourcePool in $Resources) {
## Get the variables I want
$ResourceVMs = Get-VM -Location $ResourcePool ## All of the VMs in this pool
$ResourcePoweredOnvCPUs = ($ResourceVMs | <#Where-Object {$_.PowerState -eq "PoweredOn" } |#> Measure-Object NumCpu -Sum).Sum ## Measures vCPUs for only powered on VMs in this pool
$ResourcePoweredOnvRAM = ($ResourceVMs | <#Where-Object {$_.PowerState -eq "PoweredOn" } |#> Measure-Object MemoryMB -Sum).Sum ## Measures Memory in MB for only powered on VMs in this pool
$ResourceCPUCores = ($ResourceVMs | <#Where-Object {$_.PowerState -eq "PoweredOn" } |#> Measure-Object NumCpu -Sum).Sum ## Measures vCPU Cores for only powered on VMs in this pool
$resourceCurrentCPUShares = $ResourcePool.NumCpuShares
$RresourceCurrentRAMShares = $ResourcePool.NumMemShares
## Run the size selection function
Select-size
## Calculate pool shares based on information read from pool multiplied by information read from user
$ResourceCPU_shares_new = [int]$CPU_shares * [int]$ResourceCPUCores
$ResourceRAM_shares_new = [int]$RAM_shares * [int]$ResourcePoweredOnvRAM
## Calculate difference between current and recommended shares
$PoolCPUdiff = [int]$resourceCurrentCPUShares - [int]$ResourceCPU_shares_new
$PoolRAMdiff = [int]$RresourceCurrentRAMShares - [int]$ResourceRAM_shares_new
Display-CPU-Result
Display-RAM-Result
}
}
}
}
Function Update-one-pool {
$cluster = $null
$input = Read-Host "Please enter the name of the resource pool"
$resourcepool = get-vapp $input
## Get my variables
$ResourceVMs = Get-VM -Location $ResourcePool ## All of the VMs in this pool
$ResourcePoweredOnvCPUs = ($ResourceVMs | <#Where-Object {$_.PowerState -eq "PoweredOn" } |#> Measure-Object NumCpu -Sum).Sum ## Measures vCPUs for only powered on VMs in this pool
$ResourcePoweredOnvRAM = ($ResourceVMs | <#Where-Object {$_.PowerState -eq "PoweredOn" } |#> Measure-Object MemoryMB -Sum).Sum ## Measures Memory in MB for only powered on VMs in this pool
$ResourceCPUCores = ($ResourceVMs | <#Where-Object {$_.PowerState -eq "PoweredOn" } |#> Measure-Object NumCpu -Sum).Sum ## Measures vCPU Cores for only powered on VMs in this pool
$resourceCurrentCPUShares = $ResourcePool.NumCpuShares
$RresourceCurrentRAMShares = $ResourcePool.NumMemShares
## Run the size selection function
Select-size
## Calculate pool shares based on information read from pool multiplied by information read from user
$ResourceCPU_shares_new = [int]$CPU_shares * [int]$ResourceCPUCores
$ResourceRAM_shares_new = [int]$RAM_shares * [int]$ResourcePoweredOnvRAM
## Calculate difference between current and recommended shares
$PoolCPUdiff = [int]$resourceCurrentCPUShares - [int]$ResourceCPU_shares_new
$PoolRAMdiff = [int]$RresourceCurrentRAMShares - [int]$ResourceRAM_shares_new
Display-CPU-Result
Display-RAM-Result
}
Function List-Resources {
write-host "Resource / parent cluster"
foreach ($Cluster in $Clusters)
{
## Get the resource pools for this cluster, omit the hidden pool named Resources
$Resources = $Cluster | Get-vApp
$Resources += $Cluster | Get-resourcepool | where-object {$_.name -notlike "*Resources*"}
foreach ($Resource in $Resources)
{
write-host -f Green $Resource.Name -NoNewline; write-host " (" -NoNewline; write-host -f Cyan $Cluster.Name -NoNewline; write-host ")"
}
}
}
function Main-menu {
write-host "======================================"
write-host -f cyan "Please make a selection."
$value = read-host "
A: Update all vApps / Resource Pools`
O: Update one vApp / Resource Pool`
L: List all vApps / Resource Pools`
X: Exit`
Please make a selection (A/O/L/X)"
Switch ($value)
{
'A' {Update-all-pools
Main-Menu}
'O' {Update-one-pool
main-menu}
'L' {List-Resources
Main-menu}
'X' {exit}
default {main-menu}
}
}
main-menu