Maybe you’re upgrading your hosts, maybe you’re trying to run a command on multiple ESXi Hosts, any which way one thing which you will have to do is enable SSH from the vCenter, and disable when you’re done – because annoying as it is, security is important! 😉 It would be especially annoying if you have to do that for dozens of ESXi hosts at a time.
So here I’ll show you guys a simple script you can run to enable and then disable SSH on multiple ESXi hosts so that you can go ahead and do your main task instead of being distracted by side quests!
Enable SSH on ESX host
$host_list = Get-Content "host_list.txt"
Connect-VIServer $vcenter
foreach($hosts in $host_list){
Get-VMHostService -VMHost $hosts | Where-Object {$_.Key -eq "TSM-SSH" } | Start-VMHostService -confirm:$false
}
Disconnect-VIServer * -confirm:$false
If you’ve followed the blog you will know I like to follow the Get-Content method to get a list of things (in this case list of ESXi Hosts) from a simple text file (separated by new lines). You can follow how to use Get-Content here
The real star of the show is this guy right here:
Get-VMHostService -VMHost $hosts | Where-Object {$_.Key -eq "TSM-SSH" } | Start-VMHostService -confirm:$false
We use the Get-VMHostService cmdlet to output the list of services that is available on the ESX host. From there we pipe that list and search for “TSM-SSH” service. Once that is filtered we pipe that service over to the Start-VMHostService cmdlet which will enable SSH on the host. This usually triggers a “Are you sure?” on the shell but since we need things as automated as possible we disable that with -confirm:$false
That’s it! Simples!
Disable SSH on ESX host
$host_list = Get-Content "host_list.txt"
Connect-VIServer $vcenter
foreach($hosts in $host_list){
Get-VMHostService -VMHost $hosts | Where-Object {$_.Key -eq "TSM-SSH" } | Stop-VMHostService -confirm:$false
}
Disconnect-VIServer * -confirm:$false
This is exactly the same as above except that instead of piping the filtered SSH service to “Start-VMHostService” cmdlet, we pipe it to “Stop-VMHostService” cmdlet. Self explanatory really (works out because I’m lazy to type at this point xD)
And that is it! It makes me feel validated if you like, share, comment if you found this useful (no pressure 😉 )! Social Media Links are below
Happy Scripting!