Did you even try the script I provided earlier?
You can't just lift out a single line without considering the context.
The trick is that the $esx variable is populated with 1 ESXi node (option 1) or all ESXi nodes (option 2).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference