-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Konfig-ESXi.psm1
234 lines (206 loc) · 8.12 KB
/
Konfig-ESXi.psm1
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
223
224
225
226
227
228
229
230
231
232
233
234
function Konfig-ESXi {
<#
.NOTES
===========================================================================
Created by: Markus Kraus
Twitter: @VMarkus_K
Private Blog: mycloudrevolution.com
===========================================================================
Changelog:
2016.12 ver 1.0 Base Release
2016.12 ver 1.1 ESXi 6.5 Tests, Minor enhancements
===========================================================================
External Code Sources:
Function My-Logger : http://www.virtuallyghetto.com/
===========================================================================
Tested Against Environment:
vSphere Version: ESXi 5.5 U2, ESXi 6.5
PowerCLI Version: PowerCLI 6.3 R1, PowerCLI 6.5 R1
PowerShell Version: 4.0, 5.0
OS Version: Windows 8.1, Server 2012 R2
Keyword: ESXi, NTP, SSH, Syslog, SATP,
===========================================================================
.DESCRIPTION
This Function sets the Basic settings for a new ESXi.
* NTP
* SSH
* Syslog
* Power Management
* HP 3PAR SATP/PSP Rule
* ...
.Example
Konfig-ESXi -VMHost myesxi.lan.local -NTP 192.168.2.1, 192.168.2.2 -syslog "udp://loginsight.lan.local:514"
.PARAMETER VMHost
Host to configure.
.PARAMETER NTP
NTP Server(s) to set.
.PARAMETER Syslog
Syslog Server to set, e.g. "udp://loginsight.lan.local:514"
DNS Name must be resolvable!
#Requires PS -Version 4.0
#Requires -Modules VMware.VimAutomation.Core, @{ModuleName="VMware.VimAutomation.Core";ModuleVersion="6.3.0.0"}
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$True, ValueFromPipeline=$False, Position=0)]
[String] $VMHost,
[Parameter(Mandatory=$true, ValueFromPipeline=$False, Position=1)]
[array]$NTP,
[Parameter(Mandatory=$true, ValueFromPipeline=$False, Position=2)]
[String] $syslog
)
Begin {
Function My-Logger {
param(
[Parameter(Mandatory=$true)]
[String]$message
)
$timeStamp = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
Write-Host -NoNewline -ForegroundColor White "[$timestamp]"
Write-Host -ForegroundColor Green " $message"
}
function Set-MyESXiOption {
[CmdletBinding()]
param(
[Parameter(Mandatory=$True, ValueFromPipeline=$False, Position=0)]
[String] $Name,
[Parameter(Mandatory=$False, ValueFromPipeline=$False, Position=1)]
[String] $Value
)
process {
$myESXiOption = Get-AdvancedSetting -Entity $ESXiHost -Name $Name
if ($myESXiOption.Value -ne $Value) {
My-Logger " Setting ESXi Option $Name to Value $Value"
$myESXiOption | Set-AdvancedSetting -Value $Value -Confirm:$false | Out-Null
}
else {
My-Logger " ESXi Option $Name already has Value $Value"
}
}
}
}
Process {
$Validate = $True
#region: Start vCenter Connection
My-Logger "Starting to Process ESXi Server Connection to $VMHost ..."
if (($global:DefaultVIServers).count -gt 0) {
Disconnect-VIServer -Force -Confirm:$False -ErrorAction SilentlyContinue
}
$VIConnection = Connect-VIServer -Server $VMHost
if (-not $VIConnection.IsConnected) {
Write-Error "ESXi Connection Failed."
$Validate = $False
}
elseif ($VIConnection.ProductLine -ne "EmbeddedEsx") {
Write-Error "Connencted System is not an ESXi."
$Validate = $False
}
else {
$ESXiHost = Get-VMHost
My-Logger "Connected ESXi Version: $($ESXiHost.Version) $($ESXiHost.Build) "
}
#endregion
if ($Validate -eq $True) {
#region: Enable SSH and disable SSH Warning
$SSHService = $ESXiHost | Get-VMHostService | where {$_.Key -eq 'TSM-SSH'}
My-Logger "Starting SSH Service..."
if($SSHService.Running -ne $True){
Start-VMHostService -HostService $SSHService -Confirm:$false | Out-Null
}
else {
My-Logger " SSH Service is already running"
}
My-Logger "Setting SSH Service to Automatic Start..."
if($SSHService.Policy -ne "automatic"){
Set-VMHostService -HostService $SSHService -Policy "Automatic" | Out-Null
}
else {
My-Logger " SSH Service is already set to Automatic Start"
}
My-Logger "Disabling SSH Warning..."
Set-MyESXiOption -Name "UserVars.SuppressShellWarning" -Value "1"
#endregion
#region: Config NTP
My-Logger "Removing existing NTP Server..."
try {
$ESXiHost | Remove-VMHostNtpServer -NtpServer (Get-VMHostNtpServer) -Confirm:$false
}
catch [System.Exception] {
Write-Warning "Error during removing existing NTP Servers."
}
My-Logger "Setting new NTP Servers..."
foreach ($myNTP in $NTP) {
$ESXiHost | Add-VMHostNtpServer -ntpserver $myNTP -confirm:$False | Out-Null
}
My-Logger "Configure NTP Service..."
$NTPService = $ESXiHost | Get-VMHostService| Where-Object {$_.key -eq "ntpd"}
if($NTPService.Running -eq $True){
Stop-VMHostService -HostService $NTPService -Confirm:$false | Out-Null
}
if($NTPService.Policy -ne "on"){
Set-VMHostService -HostService $NTPService -Policy "on" -confirm:$False | Out-Null
}
My-Logger "Configure Local Time..."
$HostTimeSystem = Get-View $ESXiHost.ExtensionData.ConfigManager.DateTimeSystem
$HostTimeSystem.UpdateDateTime([DateTime]::UtcNow)
My-Logger "Start NTP Service..."
Start-VMHostService -HostService $NTPService -confirm:$False | Out-Null
#endregion
#region: Remove default PG
My-Logger "Checking for Default Port Group ..."
if ($defaultPG = $ESXiHost | Get-VirtualSwitch -Name vSwitch0 | Get-VirtualPortGroup -Name "VM Network" -ErrorAction SilentlyContinue ){
Remove-VirtualPortGroup -VirtualPortGroup $defaultPG -confirm:$False | Out-Null
My-Logger " Default PG Removed"
}
else {
My-Logger " No Default PG found"
}
#endregion
#region: Configure Static HighPower
My-Logger "Setting PowerProfile to Static HighPower..."
try {
$HostView = ($ESXiHost | Get-View)
(Get-View $HostView.ConfigManager.PowerSystem).ConfigurePowerPolicy(1)
}
catch [System.Exception] {
Write-Warning "Error during Configure Static HighPower. See latest errors..."
}
#endregion
#region: Conf Syslog
My-Logger "Setting Syslog Firewall Rule ..."
$SyslogFW = ($ESXiHost | Get-VMHostFirewallException | where {$_.Name -eq 'syslog'})
if ($SyslogFW.Enabled -eq $False ){
$SyslogFW | Set-VMHostFirewallException -Enabled:$true -Confirm:$false | Out-Null
My-Logger " Syslog Firewall Rule enabled"
}
else {
My-Logger " Syslog Firewall Rule already enabled"
}
My-Logger "Setting Syslog Server..."
Set-MyESXiOption -Name "Syslog.global.logHost" -Value $syslog
#endregion
#region: Change Disk Scheduler
My-Logger "Changing Disk Scheduler..."
Set-MyESXiOption -Name "Disk.SchedulerWithReservation" -Value "0"
#endregion
#region: Configure HP 3PAR SATP/PSP Rule
My-Logger "Configure HP 3PAR SATP/PSP Rule"
$esxcli2 = Get-ESXCLI -VMHost $ESXiHost -V2
$arguments = $esxcli2.storage.nmp.satp.rule.add.CreateArgs()
$arguments.satp = "VMW_SATP_ALUA"
$arguments.psp = "VMW_PSP_RR"
$arguments.pspoption = "iops=100"
$arguments.claimoption = "tpgs_on"
$arguments.vendor = "3PARdata"
$arguments.model = "VV"
$arguments.description = "HP 3PAR custom SATP Claimrule"
try {
$esxcli2.storage.nmp.satp.rule.add.Invoke($arguments)
}
catch {
Write-Warning "Error during Configure HP 3PAR SATP/PSP Rule. See latest errors..."
}
#endregion
}
}
}