PowerCLI: Starting and Stopping the SSH service on multiple ESXi hosts

I was recently looking at enabling the SSH service on multiple ESXi hosts in a cluster to invoke a change using remote access, in order to do so I was able to start and stop the SSH service on each ESXi host using PowerCLI.

Firstly, I established a connection to the vCenter Server and created a collection of ESXi hosts from a specific cluster.

if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) 
    {
    Add-PSSnapin VMware.VimAutomation.Core | Out-Null 
    }

Connect-VIServer server1.domain.local

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

For each ESXi host returned in the collection we will retrieve the service we require to restart using the ‘Get-VMHostService’ cmdlet and filter to retrieve the key name which matches the SSH service and pass this to the ‘Start-VMHostService’ cmdlet to start the service on each ESXi Host.

ForEach ($VMHost in $VMHosts) 

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

You can also stop the SSH service as above, but to remove the requirement for user interaction you will be required to specify the Command switch to invoke the ‘Stop-VMHostService’ cmdlet without confirmation.

ForEach ($VMHost in $VMHosts) 

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

3 thoughts on “PowerCLI: Starting and Stopping the SSH service on multiple ESXi hosts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s