Backtrack:  
 
by lunarg on December 20th 2018, at 11:17

Starting or stopping the SSH service on multiple ESXi hosts can be a tedious job when having to do this via the vSphere (Web)Client. Fortunately, you can also use PowerCLI to start/stop services quickly. With a little scripting, you can expand this to start/stop services on a set of hosts, a cluster, or the entire vCenter.

First, start PowerCLI and make a connection to the vCenter. For automation, you can use something like this (note that you have to add code for credentials, if needed):

if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) 
{
    Add-PSSnapin VMware.VimAutomation.Core | Out-Null 
}
Connect-VIServer vcenter.domain.local

Once that's done, you can gather a list of hosts to work on:

$VMHosts = Get-Cluster "MyCluster" | Get-VMHost

To start the SSH service, use the list (array variable $VMHosts) to start the service on:

ForEach ($VMHost in $VMHosts) 
{ 
    Start-VMHostService -HostService (Get-VMHostService -VMHost $VMHost.Name | Where {$_.Key -eq "TSM-SSH"})
}

To stop the SSH service:

ForEach ($VMHost in $VMHosts) 
{ 
    Stop-VMHostService -HostService (Get-VMHostService -VMHost $VMHost.Name | Where {$_.Key -eq "TSM-SSH"}) -Confirm:$false 
}

Use -Confirm:$false to avoid the confirmation prompt on each host.