Wednesday, February 6, 2019

Schedule to Disable ESXi SSH

There's been something that continuously happens in the VMware environment that I manage where people will turn on SSH management of the ESXi hosts and leave it running. We could suppress the warning, or just go on living our lives paying no attention to it. But something that is more fun is to run a script to disable it on a schedule.

Now, this isn't anything new ... There are many posts on how to enable it and disable it with powercli. This is just something that looks at all the hosts in the environment then disables SSH only if it is enabled, and runs on an daily schedule

#############################
# Connect to vCenter        #
#############################

Import-Module VMware.VimAutomation.Core
$viserver = 'vcenter-server.domain.com'
$cred = Import-Clixml C:\scripts\creds\vc-credentials.xml

Connect-VIServer -Server $viServer -Credential $cred


#############################
# Disable Running SSH       #
#############################

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

Simple, now SSH won't be running unless it is necessary.