Monthly Archives: October 2015

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")

Change Virtual Machine memory with VMware PowerCLI

Changing memory assigned to a virtual machine, you may think will be a quick 2 minute job. By default this task can only be performed whilst the virtual machine is powered off. If the guest vm supports hot add then this can be done when the machine is online.

To enable Hot add on a virtual machine you can follow this guide from VMware Change Memory Hot Add Settings in the vSphere Web Client

Turning a VM off during the business day might not go down to well with your customers/users. That leaves us with carrying out the change out of hrs, I’m your like me and prefer to keep your evening as your own rather than working.

The following can be saved as a ps1 script and scheduled to run out of hrs in a maintenance window. If you specify the email setting in the script an email will be generated once completed to notify that the change has been completed.

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 180915
##Task Change VM memory
##VM name
$VM2change = "test-server"
##Email Settings
$emailServer = "192.168.1.1"
$sender = "powercli@vsphere.local"
$recipients = "admin@vsphere.local"
##Load VMware PS plugin
Add-PSSnapin VMware.VimAutomation.Core
##Connect to vCenter
connect-viserver -server vcenter.vsphere.local -User administrator@vsphere.local -Password Password123
###########################Start- Custom Task #########################
$beforechange = (GET-VM -Name $VM2change|FT -auto MemoryGB|out-string)
##Stop VM
GET-VM -Name $VM2change| Stop-VMGuest -Confirm:$False
start-sleep -s 180
##Change Memory
GET-VM -Name $VM2change| set-vm -MemoryGB 28 -Confirm:$False
##Start VM
GET-VM -Name $VM2change| Start-VM -Confirm:$False
$afterchange = (GET-VM -Name $VM2change|FT -auto MemoryGB|Out-String)
##ping VM
start-sleep -s 120
$isalive= (Test-Connection -ComputerName $VM2change -count 1|Out-String)
###########################End- Custom task #########################
##Compose eMail and send
$body = @" 
Memory Before,$beforechange.
Memory After, $afterchange.
Is VM up??, $isalive
"@
send-mailmessage -from $sender -to $recipients -subject "VM Memory Change $VM2change" -Bodyashtml "$body" -smtpserver $EmailServer