PowerShell Friday: Enabling SSH with PowerCLI
Even if you have all the graphic tools that you want to manage your virtual environment, sometimes you still want to use command line utilities for maximum control or for something that isn’t really supported.
There are more than one way to enable SSH on your hosts. One of them is via the DCUI (Direct Console User Interface). One of the ways to do that remote is with SSH. Enabling SSH can be done from the GUI, but since that’s not the goal of PowerShell Friday we’re going to do this the PowerCLI way.
What is SSH
SSH, or Secure Shell, is a way to get into a ESXi or Linux host throug the network, so you can enter commands and get the output without entering your data center. SSH encrypts the connection so no clear text information is sent to and from the ESXi host.
Enabling SSH
If you want to enable SSH on all hosts in your vCenter, you can use the oneliner below.
1 |
Get-VMHost | Foreach {Start-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"} )} |
If you want to filter which hosts you want to enable SSH on, specify them on the Get-VMHost:
1 |
Get-VMHost -Name MyHosts*| Foreach {Start-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"} )} |
Check which hosts still have SSH enabled.
1 |
Get-VMHost | Get-VMHostService | Where { $_.Key -eq "TSM-SSH" } |select VMHost, Label, Running |
By the way, if you were wondering what TSM stands for, it is “Technical Support Mode”.
Disabling SSH
If you want to disable SSH on all hosts still running SSH, you can use the following:
1 |
Get-VMHost | Foreach {Stop-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"} )} |
Shorter/faster
The oneliners I used above are retrieving the objects for hosts with Get-VMHost. After that they get processed by the Get-VMHostservice. The oneliner can be simplified by using the host directly in the Get-VMHostService cmdlet. It seems a little bit faster.
1 |
Get-VMHostService -VMHost "esx01.local" | Where-Object {$_.Key -eq "TSM-SSH"} |
The reason that I’m still using the first method is that I really don’t know. I want to say that it is more logical than the latter, but it isn’t. “Getting the object for VMHost and then looping through it, feeding it through the Stop-VMHostService with parameters from Get-VMHostService to stop them” isn’t more logical than “Get the services from host esx01.local, select the SSH service and stop it”.
1 |
Get-VMHostService -VMHost * | Where-Object {$_.Key -eq "TSM-SSH" } | Stop-VMHostService |
Works just as good as the first one.
What are you using?
Now I told you my oneliner, which version are you using or preferring? Please let me know in the comments section below.
Other articles in the series PowerShell Friday:
- PowerShell Friday: Getting Started with PowerShell and PowerCLI
- PowerShell Friday: Connecting to vCenter
- PowerShell Friday: Starting VMs
- PowerShell Friday: stopping VMs
- PowerShell Friday: Creating Virtual Machines
- PowerShell Friday: Snapshots
- PowerShell Friday: Adding CPU’s with PowerCLI
- PowerShell Friday: Adding Memory with PowerCLI
- PowerShell Friday: ExtensionData
- PowerShell Friday: Retrieving IP addresses for VMs
- PowerShell Friday: Copying files with Copy-VMGuestFile
- PowerShell Friday: Setting Reservations with PowerCLI
- PowerShell Friday: Enabling SSH with PowerCLI
- PowerShell Friday: Christmas Special
- PowerShell Friday: Configuring vSphere MTU Size
- PowerShell Friday: Load PowerCLI from your own script
- PowerShell Friday: Using the Cisco ACI API
Related Posts:
- PowerShell Friday: Configuring vSphere MTU Size by Martijn Smit
- PowerShell Friday: Creating Virtual Machines by Sander Martijn
- PowerShell Friday: stopping VMs by Anne Jan Elsinga
- PowerShell Friday: Connecting to vCenter by Anne Jan Elsinga
- PowerShell Friday: Getting Started with PowerShell and… by Anne Jan Elsinga
Anne Jan Elsinga
Related Posts
1 Comment
Leave a Reply Cancel reply
You must be logged in to post a comment.
Thanks for sharing here is another way to do the same thing, find it more readable.
Get-VMHost myHost | Get-VMhostService | Where Label -eq SSH | Start-VMhostService # Requires PSVersion 3 or greater