VMware PowerCLI – Enable/Disable SSH Per vSphere Cluster

The following script allows the user to Enable/Disable SSH at a per vSphere cluster level. You may download the ‘SSH.ps1‘ script here: SSH.ps1 (Remove the .doc extension).

The script will begin by prompting you to enter the vCenter Host IP Address, username and password. A list of all the available clusters residing in vCenter will be returned, followed by a prompt to enter the cluster name you wish to Enable/Disable SSH for:

ssh1

Once you have completed the tasks on the hosts that required SSH Access, you may then return to the running script and type option ‘y’ in order to again disable SSH on all the hosts in the specified cluster:

ssh2

 

SCRIPT:

########### vCenter Connectivity Details ###########

Write-Host “Please enter the vCenter Host IP Address:” -ForegroundColor Yellow -NoNewline

$VMHost = Read-Host

Write-Host “Please enter the vCenter Username:” -ForegroundColor Yellow -NoNewline

$User = Read-Host

Write-Host “Please enter the vCenter Password:” -ForegroundColor Yellow -NoNewline

$Pass = Read-Host

Connect-VIServer -Server $VMHost -User $User -Password $Pass

########### Please Enter the Cluster to Enable SSH ###########

Write-Host “Clusters Associated with this vCenter:” -ForegroundColor Green

$VMcluster = ‘*’

ForEach ($VMcluster in (Get-Cluster -name $VMcluster)| sort)

{

Write-Host $VMcluster

}

Write-Host “Please enter the Cluster to Enable/Disable SSH:” -ForegroundColor Yellow -NoNewline

$VMcluster = Read-Host

########### Enabling SSH ###########

Write-Host “Ready to Enable SSH? “ -ForegroundColor Yellow -NoNewline

Write-Host ” Y/N:” -ForegroundColor Red -NoNewline

$SSHEnable = Read-Host

if ($SSHEnable -eq “y”) {

Write-Host “Enabling SSH on all hosts in your specified cluster:” -ForegroundColor Green

Get-Cluster $VMcluster | Get-VMHost | ForEach {Start-VMHostService -HostService ($_ | Get-VMHostService | Where {$_.Key -eq “TSM-SSH”})}

}

########### Disabling SSH ###########

Write-Host “Ready to Disable SSH? “ -ForegroundColor Yellow -NoNewline

Write-Host ” Y/N:” -ForegroundColor Red -NoNewline

$SSHDisable = Read-Host

if ($SSHDisable -eq “y”) {

Write-Host “Disabling SSH” -ForegroundColor Green

Get-Cluster $VMcluster | Get-VMHost | ForEach {Stop-VMHostService -HostService ($_ | Get-VMHostService | Where {$_.Key -eq “TSM-SSH”}) -Confirm:$FALSE}

}

 

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 )

Google+ photo

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

Connecting to %s