Configure Syslog on Local Datastore with PowerCLI

Configuring a syslog folder is convenient for troubleshooting ESXi. But it’s a pain you-know-where to configure manually across all your ESXi servers. Luckily, PowerCLI can help out. This script creates a folder on the local datastore and configures ESXi to write the syslog in that folder.
Enjoy!
Hugo

#Folder name you like
$syslogFolderName = "syslog"
 
#Connect to vCenter
Connect-VIServer vcenter.domain.local
 
#Loop through hosts
ForEach ($vmhost in Get-VMHost)
	{
	#Get only the first part of the FQDN (only if you used the FQDN to add the ESX server to vCenter)
	$hostname = $vmhost.name.split(".")[0].ToUpper()
 
	#Local datastore name (equal to hostname in my case)
	$datastorename = $hostname
 
	#Make the local datastore accessible as a PSdrive
	New-PSDrive -Name $hostname -Root \ -PSProvider VimDatastore -Datastore (Get-Datastore $datastorename) -Scope global
 
	#Access the new PSDrive
	Set-Location $hostname":"
 
	#Create the syslog folder
	New-Item $syslogFolderName -ItemType directory
 
	#Set the advanced parameter to configure the syslog on the local datastore
	$value = "[$datastorename] $syslogFoldername/$hostname.log"
	Set-VMHostAdvancedConfiguration -VMHost $vmhost -Name "Syslog.Local.DatastorePath" -Value $value
 
	#Cleanup
	Set-Location $PSHOME
	Remove-PSDrive $hostname
	Clear-Variable hostname -ErrorAction SilentlyContinue
	Clear-Variable datastorename -ErrorAction SilentlyContinue
	Clear-Variable value -ErrorAction SilentlyContinue
	}
 
#Disconnect from vCenter
Disconnect-VIServer -confirm:$false

Leave a Reply

Close Menu