I would like some help with scripting the following steps to cerate a scratch partition on all hosts that do not have one and then reprot back to me which hosts had to have one created.
all of my ESXi hosts have a boot LUN with space, all the boot LUNs are named with the same naming conveiton i.e. "hostname-localstorage"
I'd like to come up with a script that will check each host in a cluster for a scaratch partition, if it does not have one create the folder ".locker-hostname" on local storrage and then set the scratch partition to the new location. Then report back to me which hosts already had a scratch partition and which ones it had to create a scratch parition for so I can then plan a good time to put the hosts with newly created scratch partitions into mainenance mode and reboot them.
I pasted the manual way to do this below. I'd appreciate any help anyone can give and it willbe much appreciated. I am trying to avoid doing the below 1000 times. Thanks in advance...
connect-viserver ESXHostnameOrIP
Get-Datastore
New-PSDrive -Name "mounteddatastore" -Root \ -PSProvider VimDatastore -Datastore (Get-Datastore "DatastoreName")
Set-Location mounteddatastore:
New-Item "DirectoryName" -ItemType directory
New-Item ".locker-ESXHostname" -ItemType directory
ScratchConfig.ConfiguredScratchLocation
configuration option using the command:Get-VMHostAdvancedConfiguration -Name "ScratchConfig.ConfiguredScratchLocation"
ScratchConfig.ConfiguredScratchLocation
configuration option, specifying the full path to the directory created in Step 6, using the command:Set-VMHostAdvancedConfiguration -Name "ScratchConfig.ConfiguredScratchLocation" -Value "/vmfs/volumes/DatastoreName/DirectoryName"
Set-VMHostAdvancedConfiguration -Name "ScratchConfig.ConfiguredScratchLocation" -Value "/vmfs/volumes/Datastore1/.locker-ESXHostname"
Mike P
I use something similar to the below script.
I think you will find your steps back in there.
$configName = 'ScratchConfig.ConfiguredScratchLocation'
$mode,$warn = Get-PowerCLIConfiguration | %{$_.DefaultVIServerMode,$_.DisplayDeprecationWarnings}
Set-PowerCLIConfiguration -DefaultVIServerMode "Multiple" -DisplayDeprecationWarnings:$false
Get-VMHost | where {$_.ExtensionData.Config.Product.ProductLineId -eq "embeddedEsx"} | %{
Connect-VIServer -Server $_.Name -User root -Password MyPswd
$scratch = (Get-VMHostAdvancedConfiguration -VMHost $_)[$configName]
$tgtName = ".locker-" + $_.Name.Split('.')[0]
if($scratch -notmatch ($tgtName + "$")){
$ds = Get-Datastore -VMHost $_ | where {$_.Name -match "localstorage"}
if($ds){
Set-Location -Path $ds.DatastoreBrowserPath
New-Item $tgtName -ItemType directory
Set-VMHostAdvancedConfiguration -Name $configName -Value ('/vmfs/volumes/' + $ds.Name + '/' + $tgtName) -Confirm:$false
Set-VMHost -VMHost $_ -State 'Maintenance' -Confirm:$false
Restart-VMHost -VMHost $_ -Confirm:$false
Set-VMHost -VMHost $_ -State 'Connected' -Confirm:$false
}
}
Disconnect-VIServer -Server $_.Name
}
Set-PowerCLIConfiguration -DefaultVIServerMode $mode -DisplayDeprecationWarnings:$warn
It uses the DatastoreBrowserPath as Vitali suggested.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I guess i could start with a script to list each host and its current if any scratch partition location
Hi Mike,
It seems that your approach is the right one. I found this article: http://blogs.vmware.com/esxi/2011/03/ops-changes-part-5-scratch-partition.html . It explains several ways to configure the scratch partition and one of them is the PowerCLI way, which looks like yours.
Several tips about your code:
You can omit creation of PSDrive, because PowerCLI provides two automatically created drives - vmstore and vmstores. Just check them to see what they offer.
The really good thing is that Datastore objects have property DatastoreBrowserPath, which points to the root of given datastore based on vmstores drive:
PS> $ds = Get-Datastore <datastoreName>
PS> $ds.DatastoreBrowserPath
vmstores:\myHost@443\ha-datacenter\Storage1So your steps 3, 4, 5, 6 could look like this:
PS> $ds = Get-Datastore "DatastoreName"
PS> cd $ds.DatastoreBrowserPath
PS> mkdir ".local....."
Vitali
PowerCLI Team
I use something similar to the below script.
I think you will find your steps back in there.
$configName = 'ScratchConfig.ConfiguredScratchLocation'
$mode,$warn = Get-PowerCLIConfiguration | %{$_.DefaultVIServerMode,$_.DisplayDeprecationWarnings}
Set-PowerCLIConfiguration -DefaultVIServerMode "Multiple" -DisplayDeprecationWarnings:$false
Get-VMHost | where {$_.ExtensionData.Config.Product.ProductLineId -eq "embeddedEsx"} | %{
Connect-VIServer -Server $_.Name -User root -Password MyPswd
$scratch = (Get-VMHostAdvancedConfiguration -VMHost $_)[$configName]
$tgtName = ".locker-" + $_.Name.Split('.')[0]
if($scratch -notmatch ($tgtName + "$")){
$ds = Get-Datastore -VMHost $_ | where {$_.Name -match "localstorage"}
if($ds){
Set-Location -Path $ds.DatastoreBrowserPath
New-Item $tgtName -ItemType directory
Set-VMHostAdvancedConfiguration -Name $configName -Value ('/vmfs/volumes/' + $ds.Name + '/' + $tgtName) -Confirm:$false
Set-VMHost -VMHost $_ -State 'Maintenance' -Confirm:$false
Restart-VMHost -VMHost $_ -Confirm:$false
Set-VMHost -VMHost $_ -State 'Connected' -Confirm:$false
}
}
Disconnect-VIServer -Server $_.Name
}
Set-PowerCLIConfiguration -DefaultVIServerMode $mode -DisplayDeprecationWarnings:$warn
It uses the DatastoreBrowserPath as Vitali suggested.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Team , Why is the directory we have to create .locker-ESXHostname and not just "ESXHostname" ?
I don't like implementing without understanding. Still a novice .
Also I found the the LET Get-VMHostAdvancedConfiguration has been phased out . So have to use get-AdvancedSetting | Set-AdvancedSettng now .
LucD
Can we use a NFS mount for this over local disk ?
If yes, Can you help modify this scrip ? idea is in case the host down in that cluster we will have shared storage anyways so this shared NAS mount say will be useful. Kindly suggest
That should work afaik.
There is KB2004978 you will have to verify if that can/will impact you.
Since the script uses the datastorename to define the scratch partition (independent of the type), there shouldn't be any changes to be made to the script.
You will have to update the following line to located the desired datastore.
$ds = Get-Datastore -VMHost $_ | where {$_.Name -match "localstorage"}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD
LucD
in above the code below needs some modifications
...
Restart-VMHost -VMHost $_ -Confirm:$false | |
Set-VMHost -VMHost $_ -State 'Connected' -Confirm:$false .. |
..
As soon as the host is restarted the next command get trigger & fails.
Kindly Suggest.
You should build in a loop that waits till the host is available again in that case
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference