I was recently tasked with configuring all ESXi hosts within a number of vCenter environments to use a Syslog Server. Each of these environments contained numerous clusters and ESXi hosts. Too many to manually want to configure a Syslog Server by hand. Using our lab environment I played around with a few different ways of quickly pushing out some Syslog settings via PowerCLI.
I came across two different PowerCLI cmdlets that would do the job. The first was Set-AdvancedSetting and the second was Set-VMHostAdvancedConfiguration. The latter being a deprecated cmdlet. Both cmlets do the job equally well. Ideally you would want to be using the newer Set-AdvancedSetting cmdlet rather than the deprecate one.
Where I didn’t like the newer Set-AdvancedSetting was that it would ask for confirmation before making a change. I didn’t fully realise how annoying this would be till after I had wrote my script. Once a connection to a vCenter is made in PowerCLI the script I created prompts for a Syslog Server then gets all ESXi hosts in the vCenter, applies the Syslog Server value, reloads syslog, and finally opens the syslog ports. All simple and basic except that you will be prompted to apply the syslog value to each host. Fine if you’re very cautious or if you want to omit specific hosts.
Perform operation?
Modifying advanced setting ‘Syslog.global.logHost’.
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is “Y”):y
I then decided to test out Set-VMHostAdvancedConfiguration and found I preferred this cmdlet over Set-AdvancedSetting. Each time it set a syslog value on a host it would flash up a friendly yellow little warning and go ahead with the change. Much more convenient for large changes. At some point in a future PowerCLI version I assume this cmdlet won’t work but until then it works a treat.
WARNING: Set-VMHostAdvancedConfiguration cmdlet is deprecated. Use
Set-AdvancedSetting cmdlet instead.
The below code block uses the newer Set-AdvancedSetting cmdlet.
# This script will set a Syslog Server all all ESXi hosts within a vCenter once connected. # Uses the newer Get / Set-AdvancedSetting cmdlet. # This cmdlet will require confirmation for each host being modified. # Created by Mark Ukotic # 04/04/2015 Write-Host "This script will change the Syslog Server on all hosts within a vCenter, restart Syslog, and open any required ports." Write-Host $mySyslog = Read-Host "Enter new Syslog Server. e.g. udp://10.0.0.1:514" Write-Host foreach ($myHost in get-VMHost) { #Display the ESXi Host being modified Write-Host '$myHost = ' $myHost #Set the Syslog Server $myHost | Get-AdvancedSetting -Name Syslog.global.logHost | Set-AdvancedSetting -Value $mySyslog #Restart the syslog service $esxcli = Get-EsxCli -VMHost $myHost $esxcli.system.syslog.reload() #Open firewall ports Get-VMHostFirewallException -Name "syslog" -VMHost $myHost | set-VMHostFirewallException -Enabled:$true }
This second code block uses the deprecated Set-VMHostAdvancedConfiguration, which I prefer.
# This script will set a Syslog Server all all ESXi hosts within a vCenter once connected. # The deprecated Set-VMHostAdvancedConfiguration cmdlet is used. # Seems to run a little more cleaner with this cmdlet and doesn't ask for confirmation # Created by Mark Ukotic # 04/04/2015 Write-Host "This script will change the Syslog Server on all hosts within a vCenter, restart Syslog, and open any required ports." Write-Host $mySyslog = Read-Host "Enter new Syslog Server. e.g. udp://10.0.0.1:514" Write-Host foreach ($myHost in get-VMHost) { #Display the ESXi Host being modified Write-Host '$myHost = ' $myHost #Set the Syslog Server Set-VMHostAdvancedConfiguration -Name Syslog.global.logHost -Value $mySyslog -VMHost $myHost #Restart the syslog service $esxcli = Get-EsxCli -VMHost $myHost $esxcli.system.syslog.reload() #Open firewall ports Get-VMHostFirewallException -Name "syslog" -VMHost $myHost | set-VMHostFirewallException -Enabled:$true }
Does adding “-Confirm:$false” not work?
eg:
$myHost | Get-AdvancedSetting -Name Syslog.global.logHost | Set-AdvancedSetting -Value $mySyslog -Confirm:$false
Thanks Ian! That does in fact work and solve the prompt issue. All the more reason to stick with the Set-AdvancedSetting cmdlet.
Nice pick-up
Thanks for Sharing.. It helped
Why not just use the cmdlet made for setting the syslog server?
Set-VMHostSysLogServer -VMHost $myHost -SysLogServer $mySyslog -SysLogServerPort 514 -Confirm:$false
Easy to use and no deprecation issues 🙂