Powershell Script to Update all Scratch Partitions in an ESXi Cluster

Following on from an auto deploy install using SD cards on a Cisco UCS where we couldnt configure the scratch partition within an automated process so I wrote a powershell script to loop around a specific cluster and update all the host logs.

Note: This assumes that the directories are already created. I might include this in version 2 of the script.

As a standard we set the logs to /vmfs/volumes/{DatastoreName}/hostlogs/{NameofESXiHost}
eg /vmfs/volumes/datastore1/hostlogs/tstuk-esx01

The script has two variables:

ClusterName = The name of your Compute Cluster
DatastoreName = Name of your vmfs datastore in uuid format

# Variables
$ClusterName = "MyCluster"
$DatastoreName = "My_UUID"

# Loop through each host and set scratch
foreach ($VMHost in ($Cluster | Get-VMHost ) ) {
    $Shortname = ($VMHost | Get-VMHostNetwork).HostName
    write-host Updating host: $Shortname
    $VMHost | Get-AdvancedSetting -Name "ScratchConfig.ConfiguredScratchLocation" | Set-AdvancedSetting -Value "/vmfs/volumes/$DatastoreName/hostlogs/$Shortname" -Confirm:$false

    }