Hi Guys,
I need to change the scratch file location on a bunch of ESXi Hosts. A lot of Servers lately are coming with dedicated (small) hard disks for the O/S plus, disks for vSAN.
The Default location of the scratch files is, where the O/S is installed. this is causing problems so, i want to move the Scratch files to a vSAN datastore (there may not be a VMFS datastore)
I've pieced the following together but would like any input on it before running it on a Production Environment
# login to vCenter
$table = @()
$prefix = ".scratch_"
$datastore = Get-Datastore | where {$_.Type -eq "VSAN"}
foreach ($ds in $datastore) {
New-PSDrive -Location $ds -Name PSD -PSProvider VimDatastore -Root '' | Out-Null
foreach ($h in ($ds | Get-VMHost)){
$fqdn = $h.Name
$hostName = $fqdn.Split(".")[0].TrimStart("{")
$hostDS = $h | Get-Datastore | where {$_.Type -eq "VSAN"}
$scratchFolder = "$($prefix)$($hostName)"
$tableProp=[ordered]@{
'Host FQDN'=$fqdn
'Host Name'=$hostName
'VSAN DS'= $hostDS
}
# Check to see if a scratch folder for the Host exists on the vSAN DS and create it if needed
$scratchFolderPath = "/vmfs/volumes/$($hostDS.Name)/$scratchFolder"
$scratchFolderPathExists = Test-Path -Path $scratchFolderPath | Out-Null
if (-not ($scratchFolderPathExists)){
New-Item -Path "PSD:\$scratchFolder" -ItemType Directory | Out-Null
}
# identify current Scratch Folder Settings
$configuredScratch = $h | Get-AdvancedSetting -Name "ScratchConfig.ConfiguredScratchLocation"
$configuredScratchValue = $configuredScratch.Value
$currentScratch = $h | Get-AdvancedSetting -Name "ScratchConfig.CurrentScratchLocation"
$currentScratchValue = $currentScratch.Value
$tableProp.Add('Current Scratch Value', $currentScratchValue)
# set new Scratch location
$configuredScratch | Set-AdvancedSetting -Value $scratchFolderPath -Confirm:$false | Out-Null
$tableProp.Add('New Scratch Location',$scratchFolderPath)
# Place Host in Maintenance mode, reboot, exit Maintenance Mode, move to next Host
Set-VMHost $h -State Maintenance -Evacuate
Restart-VMHost $h
Start-Sleep -Seconds 900
$connectionState = Get-VMHost $h.ConnectionState
while ($connectionState -eq "Maintenance") {
Set-VMHost $h -state Connected
}
$table += New-Object -TypeName psobject -Property $tableProp
}
Remove-PSDrive -Name PSD -Confirm:$false
}
$table | Sort-Object -Property "Host FQDN" | ft -AutoSize