I just tested this script again, and for me it is working as designed.
I strongly suspect it might be related to the hosts.txt file you are using.
In my test file I have 2 lines, with on each line the FQDN os an ESXi node.
Can you check if the following returns a VMHost object for each line your .txt file ?
$varHost=Get-Content"S:\Get-Logs\hosts.txt"
foreach ($ESXin$varHost) {
Get-VMHost-Name$ESX
}