Automate Advanced Settings with ESXCLI
After my last post about NFS Performance decreasing after host reboots, I was tasked with implementing the workaround across our infrastructure. My two options are click through the GUI until my fingers bleed or automate. As you can assume by this post, we’re most definitely going with the latter. And we all know the best way to automate advanced settings is with PowerCLI!
For this task, I will be writing a quick script using PowerCLI with ESXCLI V2. If you haven’t already, check out my post on Modifying SNMP with ESXCLI V2 here. It goes a bit deeper into how Version 2 specifically works. Let’s have a look at the script as a whole before walking through it:
Automate it!
# Gather our hosts $hosts = Get-VMHost # Modify NFS Settings ForEach ($vmhost in $hosts) { $esxcli = Get-EsxCli -VMHost $vmhost -V2 $nfsargs = $esxcli.system.settings.advanced.set.CreateArgs() $nfsargs.option = "/SunRPC/MaxConnPerIP" $nfsargs.intvalue = "32" $esxcli.system.settings.advanced.set.Invoke($nfsargs) } # Output NFS settings $result = @() ForEach ($vmhost in $hosts) { $nfsget = $esxcli.system.settings.advanced.list.CreateArgs() $nfsget.option = "/SunRPC/MaxConnPerIP" $nfsupdate = $esxcli.system.settings.advanced.list.Invoke($nfsget) $result += [PSCustomObject] @{ Hostname = $vmhost Path = "$($nfsupdate | Select -ExpandProperty Path)" IntValue = "$($nfsupdate | select -ExpandProperty IntValue)" Description = "$($nfsupdate | Select -ExpandProperty Description)" } $result | Out-File -FilePath "$outputdir\NFSMaxConnPost_$clsname-$date.txt" }
And now let’s step through it to see what’s going on:
Line 02: Here we are defining our $hosts variable. In this case, I’m calling all the hosts in my connected vCenter. This can be customized for a single host, group of hosts in a cluster, etc.
Line 05: I’m starting a ForEach loop to cycle through all of the ESXi hosts in the $hosts variable.
Line 06: Ensure that we are using Get-EsxCli -V2.
Lines 07-09: Using the $nfsargs variable, I’m configuring the NFS options that I wish to change.
Line 10: Execute (or Invoke) the changes ($nfsargs variable).
Now that the changes have been made, let’s output the results to a text file so that we can validate everything was successful.
Line 15: Begin another ForEach loop to again cycle through all of my ESXi hosts.
Lines 16-17: Using the $nfsget variable, I now define the NFS options that I wish to report on.
Line 18: Execute (or Invoke).
Lines 20-24: Create a PowerShell custom object to hold my report information in the $result variable.
Line 27: Output the $result variable to a nice text file for easy reading.
And there you have it. With a few loops, and Get-EsxCli we can take a manual task and fully automate it. We still have to reboot, but that’s a task for another day.