PowerCLI - Configure syslog server in multiple ESXi
This is a quick powershell script to setup the remote syslog in all the hosts of a cluster or vCenter.
Normally this task is simplified when using the integration of VMware Log Insight/VMware Aria Operations for Logs (VRLI), however this options sometimes is not always available, for example in the extreme cases where a need of an External Load Balancer needs to be used for VRLI or if we are not even using it as our logging collector, hence sometimes we could need to setup the syslog for multiple ESXi in one go.
Script parameters
- Mandatory
- vCenter - vCenter FQDN/IP to connect too
- vCenterUsername - vCenter Username to be used
- vCenterPassword - corresponding password
- RemoteSyslog - FQDN/IP of the syslog server to use
- Optional
- cluster - Cluster name if we want to change the hosts from a single cluster
- syslogPort In case of using an alternative port, will use 514 as default
Similar to earlier posts the code is pretty simple, so we will focus in the relevant bits
- List the current status
# Show current config
$vmHosts | ForEach-Object {
Write-Host $_.Name
Get-VMHostSysLogServer -VMHost $_
}
- Set the remoteSyslog server in each ESXi
# Set syslog config in hypervisors
$vmHosts | ForEach-Object {
Write-Host $_.Name
Set-VMHostSysLogServer -SysLogServer $remoteSyslog":"$syslogPort -VMHost $_
}
- Restart syslog and set the allow rules using Get-Esxcli
# Restart syslog and set the allow rules in the ESXi
$vmHosts | ForEach-Object {
Write-Host $_.Name
(Get-Esxcli -v2 -VMHost $_).system.syslog.reload.Invoke()
(Get-Esxcli -v2 -VMHost $_).network.firewall.ruleset.set.Invoke(@{rulesetid='syslog'; enabled=$true})
(Get-Esxcli -v2 -VMHost $_).network.firewall.refresh.Invoke()
}