Saturday, January 26, 2013

PowerCLI - Starting and stopping SSH

From time to time I need to go connect on the ESXi using SSH. I know it is not best practice, still I have to do it. And the most simple task transforms into a pain - starting and stopping SSH server on several ESXi hosts. The following small piece of PowerCLI does the job just fine:
  • starting ssh service
Get-Cluster CLS01 | Get-VMHost | Get-VMHostService | Where {$_.Key -eq "TSM-SSH"} | Start-VMHostService 
  • stopping ssh service
Get-Cluster CLS01 | Get-VMHost | Get-VMHostService | Where {$_.Key -eq "TSM-SSH"} | Stop-VMHostService -Confirm:$false
Since get-vmhostservice returns all services on ESXi host, you can use this to start/stop any service.