Tag Archives: v

Start/Stop SSH access to VMWare ESXi using PowerCli

On occasion many VMware vSphere admins have to carry out a task on a ESXi host directly. Whether this is restarting the management agents, installing vibs or general administration. To do so remotely you need SSH and the ESXi shell services running.

For enhanced security the default action in ESXi 6 is for SSH and ESXi shell services to be disabled on startup.

Previously I went through the task of opening the vSphere client and enabling these manually each time. The following script provides a scripted method to stop and start these services for you. The script could be adapted to change other services and initiate secondary actions if required.

Prerequisite:

Install VMware vSphere PowerCLI, script has been tested with version 6 R1 available hereĀ vSphere PowerCLI 6.0 Release 1

All the variables are declared at the start of the script and prefixed with the dollar $ symbol.

##Created by DM 311015
##Host Services
##VM variables
$vcenter_server ="vcenter.vsphere.local"
$vcenter_user ="administrator@vsphere.local"
$vcenter_pwd ="Password123"
##Load VMware PS plugin
Add-PSSnapin VMware.VimAutomation.Core >$null
Clear-Host
###########################Start- Custom Task #########################
##Create do loop if value does eq 1,2,3,4. Exit added if option 5 selected
do
{
write-host "
------Main Menu --------
Select one of the following options
Manage Host via vCenter
option 1: Start SSH and ESXi host services via vCenter
option 2: Stop SSH and ESXi host services via vCenter
Manage Host directly
option 3: Start SSH and ESXi host services via Host
option 4: Stop SSH and ESXi host services via Host
option 5: Exit
------------------------
"
$option = Read-Host "Enter option"
if ($option -eq "1" -OR $option -eq "2" -OR $option -eq "3" -OR $option -eq "4")
 {
 write-host 
"------Host Selection --"
 $esxihost = Read-Host "Enter FQDN of ESXi host to process"
 write-host 
"-----------------------"
 switch ($option)
 {
 1
 {
 connect-viserver -server $vcenter_server -User $vcenter_user -Password $vcenter_pwd >$null
 Clear-Host
 "Start SSH and ESXi Shell service"
 Get-VMHostService -VMHost $esxihost|?{$_.Label -eq "SSH"}|Start-VMHostService |ft -AutoSize Label, Running
 Get-VMHostService -VMHost $esxihost|?{$_.Label -eq "ESXi Shell"}|Start-VMHostService |ft -AutoSize Label, Running
 }
 2
 { 
 connect-viserver -server $vcenter_server -User $vcenter_user -Password $vcenter_pwd >$null
 Clear-Host
 "Stop SSH and ESXi Shell service"
 Get-VMHostService -VMHost $esxihost|?{$_.Label -eq "SSH"}|Stop-VMHostService -confirm:$false |ft -AutoSize Label, Running
 Get-VMHostService -VMHost $esxihost|?{$_.Label -eq "ESXi Shell"}|Stop-VMHostService -confirm:$false |ft -AutoSize Label, Running
 }
 3
 { 
 $esxihost_root = Read-Host "Enter ESXi host root user"
 $esxihost_pwd = Read-Host "Enter ESXi host root password"
 connect-viserver -server $esxihost -User $esxihost_root -Password $esxihost_pwd >$null
 Clear-Host
 "Start SSH and ESXi Shell service"
 Get-VMHostService -VMHost $esxihost|?{$_.Label -eq "SSH"}|Start-VMHostService |ft -AutoSize Label, Running
 Get-VMHostService -VMHost $esxihost|?{$_.Label -eq "ESXi Shell"}|Start-VMHostService |ft -AutoSize Label, Running
 }
 4
 { 
 $esxihost_root = Read-Host "Enter ESXi host root user"
 $esxihost_pwd = Read-Host "Enter ESXi host root password"
 connect-viserver -server $esxihost -User $esxihost_root -Password $esxihost_pwd >$null
 Clear-Host
 "Stop SSH and ESXi Shell service"
 Get-VMHostService -VMHost $esxihost|?{$_.Label -eq "SSH"}|Stop-VMHostService -confirm:$false |ft -AutoSize Label, Running
 Get-VMHostService -VMHost $esxihost|?{$_.Label -eq "ESXi Shell"}|Stop-VMHostService -confirm:$false |ft -AutoSize Label, Running
 }
 }
 }
elseif ($option -eq "5" -or $option -Contains "exit" -or $option -Contains "quit")
 {write-host "Ok then good bye :)"
 exit
 }
elseif ($option -ne "1" -OR $option -ne "2" -OR $option -ne "3" -OR $option -ne "4" -OR $option -ne "5")
 {write-host 
 "Invalid Input. Please re-enter selection"
 }
}
while ($option -ne "1" -OR $option -ne "2" -OR $option -ne "3" -OR $option -ne "4")
Advertisements