/
Set-ResourcePoolGranular.ps1
174 lines (134 loc) · 6.68 KB
/
Set-ResourcePoolGranular.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
<#
.SYNOPSIS
This script balances resource pools by couting the VMs inside of them
To use this script automatically please use the following code to produce a file with the password:
New-VICredentialStoreItem -User $user_name -Password $user_password_decrypted -File "$file_location\login.creds"
.DESCRIPTION
By counting the VMs inside a resource pool this script can effectively balance resource pools according to their requirements
.PARAMETER vCenter
The vCenter to connect to
.PARAMETER Cluster
The Cluster in which the resource pools reside
.INPUTS
None
.OUTPUTS Log File
The script log file stored in <script-dir>/Set-ResourcePoolsGranular.log
.NOTES
Version: 1.2
Author: Dario Dörflinger
Creation Date: 22.10.2018
Purpose/Change: Initial script development
Added VICredentialStoreItems for automation of this script
Added granularity to account for number of vCPUs and GBs of memory instead of VMs
.EXAMPLE
./Set-ResourcePoolsGranular.ps1 -vCenter yourvcenter.yourdomain.com -Cluster yourClusterName
That is it.
#>
#---------------------------------------------------------[Script Parameters]------------------------------------------------------
Param (
#Script parameters
[Parameter(Mandatory=$true)][string]$vCenter,
[Parameter(Mandatory=$true)][string]$Cluster
)
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
#Set Error Action to Silently Continue
$ErrorActionPreference = 'Stop'
#Import Modules & Snap-ins
Import-Module PSLogging
#----------------------------------------------------------[Declarations]----------------------------------------------------------
#Script Version
$sScriptVersion = '1.2'
#Log File Info
$sLogPath = $PSScriptRoot
$sLogName = "Set-ResourcePoolsGranular.log"
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName
$myResourcePools=@(
[pscustomobject]@{name="1_veryhigh";factor=8},
[pscustomobject]@{name="2_high";factor=4},
[pscustomobject]@{name="3_normal";factor=2},
[pscustomobject]@{name="4_low";factor=1}
)
#-----------------------------------------------------------[Functions]------------------------------------------------------------
Function Connect-VMwareServer {
Param ([Parameter(Mandatory = $true)][string]$VMServer)
Begin {
Write-LogInfo -LogPath $sLogFile -Message "Connecting to vCenter Server [$VMServer]..."
}
Process {
Try {
$oCred = Get-Credential -Message 'Enter credentials to connect to vCenter Server'
#$oCred = Get-VICredentialStoreItem -File <path to file>
Connect-VIServer -Server $VMServer -Credential $oCred -ErrorAction Stop | Out-Null
}
Catch {
Write-LogError -LogPath $sLogFile -Message $_.Exception -ExitGracefully
Break
}
}
End {
If ($?) {
Write-LogInfo -LogPath $sLogFile -Message 'Completed Successfully.'
Write-LogInfo -LogPath $sLogFile -Message ' '
}
}
}
Function OptimizeResourcePools {
Param ()
Begin {
Write-LogInfo -LogPath $sLogFile -Message 'Getting all Resource Pools from Cluster...'
}
Process {
Try {
[array]$rPools = Get-ResourcePool -Location (Get-Cluster $Cluster) -ErrorAction Stop
foreach ($rPool in $rPools) {
#Do nothing for root resource pool
if ($rPool.name -ne "Resources") {
#Loop through the custom objects
foreach ($myPool in $myResourcePools) {
#if custom name matches with existing resource pool
if ($myPool.name -eq $rPool.name) {
$vCPU = Get-VM -Location $rpool | where-object { $_.ExtensionData.Config.ManagedBy.Type -ne "placeholderVm" } | Measure-Object -Property NumCpu -Sum | Select-Object -ExpandProperty Sum
$vMem = Get-VM -Location $rpool | where-object { $_.ExtensionData.Config.ManagedBy.Type -ne "placeholderVm" } | Measure-Object -Property MemoryMB -Sum | Select-Object -ExpandProperty Sum
$vMemGB = [System.Math]::Round($vMem / 1024)
$totalvms = $rpool.ExtensionData.Vm.count
Write-LogInfo -LogPath $sLogFile -Message "Total VMs in $($myPool.name): $totalvms"
Write-LogInfo -LogPath $sLogFile -Message "Total vCPUs in $($myPool.name): $vCPU"
Write-LogInfo -LogPath $sLogFile -Message "Total Memory in $($myPool.name): $vMemGB"
$rpsharesC = $myPool.factor * $vCPU
$rpsharesM = $myPool.factor * $vMemGB
# maximum value for share is 4000000
if ($rpsharesC -lt 4000000 -and $rpsharesM -lt 4000000)
{
Write-LogInfo -LogPath $sLogFile -Message "Setting CPU Shares to $rpsharesC in $($myPool.name)"
Write-LogInfo -LogPath $sLogFile -Message "Setting Memory Shares to $rpsharesM in $($myPool.name)"
#set values
Set-ResourcePool -ResourcePool $rpool.Name -CpuSharesLevel:Custom -NumCpuShares $rpsharesC -MemSharesLevel:Custom -NumMemShares $rpsharesM -Confirm:$False -ErrorAction Stop | Out-Null
#for testing purposes:
#Set-ResourcePool -ResourcePool $rpool.Name -CpuSharesLevel:Custom -NumCpuShares $rpsharesC -MemSharesLevel:Custom -NumMemShares $rpsharesM -Confirm:$False -ErrorAction Stop -WhatIf | Out-Null
}
else
{
Write-LogError -LogPath $sLogFile -Message "Value too high (above 4000000)"
}
}
}
}
}
}
Catch {
Write-LogError -LogPath $sLogFile -Message $_.Exception -ExitGracefully
Break
}
}
End {
If ($?) {
Write-LogInfo -LogPath $sLogFile -Message 'Completed Successfully.'
Write-LogInfo -LogPath $sLogFile -Message ' '
}
}
}
#-----------------------------------------------------------[Execution]------------------------------------------------------------
Start-Log -LogPath $sLogPath -LogName $sLogName -ScriptVersion $sScriptVersion
Connect-VMwareServer $vCenter
OptimizeResourcePools
Stop-Log -LogPath $sLogFile