Scripting amidst a crisis

The world is in crisis. A third of the world is in lockdown. The very fabrics of life has been shaken to its very core. An invisible enemy is on the prowl in the streets, bubbles of safety shattered down. An unfamiliar feeling for most generations who have lived in “relative” peace.

But while the world is in lockdown, while the world’s productivity has come crashing down, one portion of the population has encountered unprecedented amounts of work – and that is us, the folks who live and breathe binary. People are forced to work from home, children are supposed to study from home and Netflix usage is off the charts. People are forced to seek productivity and entertainment digitally now more than ever before.

Thus it becomes our burden to keep the world running. In a time when the companies themselves are struggling with stocks plummeting, profits decreasing and new equipment unavailable, it is up to us code monkeys to gear up. It is up to us to optimize every line of code, to squeeze every ounce of power from servers, to not just keep the lights on, but to keep them shining as bright as ever!

So on wards Engineers. You may not have the blazing fast internet you get at office, nor the multiple monitors, the uninterrupted power or the unlimited supply of coffee… but it’s our time to give back! Script as much as possible to offset the difficulties. It might be too laggy to click, but its never too laggy for command line. Automate as much as possible so that you don’t have to worry about the little things. Keep the big picture in mind.

So on wards Engineers. Keep the world running.

Enable and Disable ESXi Host SSH with PowerCLI | VMHostService Cmdlet

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!