As per VMware KB1033696 the SD card can't be used to store scratch partition. The main purpose of the scratch partition is to store logging files and to provide space for vm-support output.
So, the normal practice is to use shared storage (VMFS/NFS) as a scratch location. The problem is that the configuration of the scratch location is not automated in the existing vSphere. So you have to manually create folder for each of the ESXi host and configure each ESXi host to use that folder.
This can be quite time-consuming and boring tasks when you have to do it for hundred of servers.
This can be quite time-consuming and boring tasks when you have to do it for hundred of servers.
To make things worse Host Profiles do not let you configure scratch location too.
I had some time last week and thought it was a good chance to have fun with PowerCLI and automate the scratch configuration for ESXi hosts.
So here is overview of what the script does:
- Connects to vCenter
- Collects the list of ESXi hosts in the cluster. Very often storage is not shared across multiple compute clusters so I decided to use cluster, not a datacenter, as a configuration target.
- Checks if there is a designated scratch folder for each of the clusters and creates if it doesn't exist
- Checks if the ESXi host configured with scratch location and if it points to the right datastore and folder.
- If ESXi is not configured yet or points to the wrong directory the correct setting will be applied.
- Provides a list of the ESXi servers to be rebooted for the configuration change to take effect
There are a couple of thing you have to do before running the script:
- Identify the datastore to be used to store scratch folders
- In that datastore create a folder where the script will create a scratch folder per each host
The syntax is as following:
.\scratch.ps1 -vCenter vCenter_Name -cluster Cluster_Name -datastore Datastore_Name -folder Folder_Name
for example
.\scratch.ps1 -vCenter lab-vc-01.lab.local -cluster HA -datastore ISO -folder Scratch
* I had to add folder as input parameter because I couldn't make the script land into the correct folder with New-PSdrive commandlet
You can go even further by taking advantage of Windows Task Scheduler to run this script on a daily basis to ensure all servers are consistently configured.
Let me know how it worked for you.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Collect the vCenter, Cluster and scratch_datastore | |
Param([String]$vCenter, [String]$Cluster, [String]$Datastore, [String]$Folder) | |
#Function to use multiple colors in one command | |
function Write-Color([String[]]$Text, [ConsoleColor[]]$Color) { | |
for ($i = 0; $i -lt $Text.Length; $i++) { | |
Write-Host $Text[$i] -Foreground $Color[$i] -NoNewLine | |
} | |
Write-Host | |
} | |
#defining array variables | |
$vmhost_array =@() | |
$dir = @() | |
$reboot_servers = @() | |
#Validating input | |
if (!$vCenter){ | |
Write-Color -Text "Please provide valid vCenter name using ","'-vCenter' ","option, exiting.." -Color Gray,Red,Gray | |
exit | |
} | |
if (!$Cluster){ | |
Write-Color -Text "Please provide valid cluster name using ","'-Cluster' ","option, exiting.." -Color Gray,Red,Gray | |
exit | |
} | |
if (!$Datastore){ | |
Write-Color -Text "Please provide valid Datastore name using ","'-Datastore' ","option, exiting.." -Color Gray,Red,Gray | |
exit | |
} | |
if (!$Folder){ | |
Write-Color -Text "Please provide valid scratch folder name using ","'-Folder' ","option, exiting.." -Color Gray,Red,Gray | |
exit | |
} | |
cls | |
#Getting the path of the script | |
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition | |
#Collecting credentials for vCenter | |
do {$user = Read-Host -Prompt 'Enter username for vCenter' | |
$pass = Read-Host -AsSecureString -Prompt 'Enter password for vCenter' | |
$cont = Read-Host -Prompt 'Type y to continue'} | |
while ($cont -ne 'y') | |
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $pass | |
#Connecting to vCenter server | |
Write-Color -Text "`nConnecting to ", $vCenter -Color Gray, Red | |
Connect-VIserver -Server $vCenter -Credential $cred | Out-Null | |
#Validating connection to vCenter | |
if(!$DefaultVIServers){ | |
Write-Color -Text "`nConnection to ",$vCenter," failed, exiting.." -Color Yellow,Red,Yellow | |
exit | |
} elseif($DefaultVIServer.name -ne $vCenter){ | |
Write-Color -Text "Connected to wrong vCenter ", $DefaultVIServer.name ", exiting.." -Color Yellow,Red,Yellow | |
} else { | |
Write-Color -Text "Connection to vCenter ", $vCenter, " succeeded" -Color Green,Red,Green | |
Write-Host | |
} | |
#Getting the list of the ESXi hosts in the $Cluster | |
$vmhost_array = get-cluster -name $cluster | Get-VMhost | |
#Getting Scratch datastore UUID | |
$ds_view = Get-View (Get-View (Get-VMHost -Name $vmhost_array[0]).ID).ConfigManager.StorageSystem | |
foreach ($vol in $ds_view.FileSystemVolumeInfo.MountInfo) { | |
if ($vol.Volume.Name -eq $datastore) { | |
$datastore_uuid = $vol.Volume.Uuid | |
} | |
} | |
#Mounting datastore to be used as a scratch location | |
New-PSDrive -Name "DS" -Root \ -PSProvider VimDatastore -Datastore (Get-VMHost -Name $vmhost_array[0] | Get-Datastore $datastore) | out-null | |
Set-Location DS:\$folder | |
#Collect the list of the folders | |
$dir = dir | |
#Check if the scratch folders exist for the ESXi hosts and create missing folders | |
Foreach ($vmhost in $vmhost_array) | |
{ | |
If ($dir.name -contains $vmhost.name){ | |
Continue | |
} | |
else{ | |
Write-Color -Text "`n Creating scratch folder for ", $vmhost -Color Green,Red | |
mkdir $vmhost | out-null | |
} | |
} | |
#Check if the ESXi host is already configured with correct scratch location | |
Foreach ($vmhost in $vmhost_array){ | |
$row = '' | Select Server_Name | |
$configured_scratch = (Get-VMhost $vmhost | Get-AdvancedSetting -Name "ScratchConfig.ConfiguredScratchLocation").value | |
$current_scratch = (Get-VMhost $vmhost | Get-AdvancedSetting -Name "ScratchConfig.CurrentScratchLocation").value | |
$correct_scratch = "/vmfs/volumes/"+$datastore_uuid+"/"+$folder+"/"+$vmhost | |
# Write-Host "`n Correct scratch is" $correct_scratch | |
# Write-Host "`n Configured Scratch on" $vmhost "is" $configured_scratch | |
# Write-Host "`n Current Scratch on" $vmhost "is" $current_scratch | |
If (($configured_scratch -eq $correct_scratch) -and ($current_scratch -eq $correct_scratch)) { | |
Write-Color -Text "`n ESXi host ", $vmhost, " was already configured with the correct scratch location" -Color Green,Red,Green | |
} elseif($configured_scratch -eq $correct_scratch) { | |
Write-Color -Text "`n The ESXi host", $vmhost, " was already configured correctly, `n but it hasn't been restared after the configuration change" -Color Yellow,Red,Yellow | |
$row.Server_Name = $vmhost.Name | |
$reboot_servers += $row | |
} else { | |
Get-VMhost $vmhost | Get-AdvancedSetting -Name "ScratchConfig.ConfiguredScratchLocation" |Set-AdvancedSetting -Value $correct_scratch -Confirm:$false |out-null | |
Write-Host -Fore:Red "`n ESXi host" $vmhost "is configured with the correct scratch location" | |
$row.Server_Name = $vmhost.Name | |
$reboot_servers += $row | |
} | |
} | |
#Provide output with the list of ESXi servers to be rebooted for the configration change to take effect | |
Write-Host -Fore:Green "`n The configuration of the scratch location for ESXi servers in cluster" $cluster "is complete" | |
Write-Host -Fore:Green "`n The following ESXi hosts have to be rebooted for the configuration change to take effect:" | |
foreach ( $server in $reboot_servers ) { | |
Write-Host -Fore:Red `n $server.Server_Name | |
} | |
#Exporting the list of ESXi hosts to be rebooted | |
$exportfilename = "Servers_to_reboot.csv" | |
$exportfilepath = Join-Path -Path $scriptPath -ChildPath $exportFileName | |
$reboot_servers | Export-Csv -Path $exportFilePath -NoTypeInformation -Force | |
Write-Host "`n This list of these servers has also been exported to" $exportfilepath | |
#Change location back to original | |
Set-Location $scriptPath | |
#Disconnecting from vCenter | |
Disconnect-viserver -Confirm:$false |
Great script. I tried on 6.5 and found that I had to add -server $vcenter to Scratchconfig lines. I did a replace ScratchLocation" with ScratchLocation" -server $vcenter and all worked well.
ReplyDeletethe section that converts the folder name to UUID is not working in 6.5, but if you simply use the datastore name in the string, and apply that in the syslog setting, the host will automatically convert the UUID
ReplyDeleteNice script!
ReplyDelete