Thursday, January 28, 2016

Script to automate to Keep those services on even after reboot in your ESXi

Now for some reason when we were building a data-center for a client they wanted some services to be kept on and running even after reboot of their esxi hosts. This is not a problem if you just have some 10 hosts but what if this datacenter has 100 hosts? I definitely don't want to go to each host and enable them for each host if you want to retain your fingers to do something else other than clicking the mouse.
Here is a quick and dirty way to get that done.
let us run
get-vmhost | get-vmhostservice and you get something like this

Key                  Label                          Policy     Running  Required
---                  -----                          ------     -------  --------
DCUI                 Direct Console UI              on         True     False  
TSM                  ESXi Shell                     on         True     False  
TSM-SSH              SSH                            on         True     False  
lbtd                 Load-Based Teaming Daemon      on         True     False  
lwsmd                Active Directory Service       off        False    False  
ntpd                 NTP Daemon                     automatic  True     False  
pcscd                PC/SC Smart Card Daemon        off        False    False  
sfcbd-watchdog       CIM Server                     on         False    False  
snmpd                SNMP Server                    on         False    False  
vmsyslogd            Syslog Server                  on         True     True   
vprobed              VProbe Daemon                  off        False    False  
vpxa                 VMware vCenter Agent           on         True     False  
xorg                 X.Org Server                   on         False    False 

Now let us filter it out to the one that we want. Let us select just the key of the output and ooyah, this is what we get

PS C:\windows\system32> get-vmhost | get-vmhostservice | select key


Let us say we just want to deal with SSH here and that is the service we want it to be running all the time.

PS C:\windows\system32> get-vmhost | get-vmhostservice |  where Key -EQ TSM-SSH

Key                  Label                          Policy     Running  Required
---                  -----                          ------     -------  --------
TSM-SSH              SSH                            on         True     False

 Now we want to start the service, so let us do it.

get-vmhost | get-vmhostservice | where Key -EQ TSM-SSH | Start-VMHostService

 but if we want it to remain on even after reboots then we have to make it as a policy to keep it on. So
get-vmhost | Get-VMHostService | where Key -EQ TSM-SSH | Start-VMHostService | Set-VMHostService -Policy On

 So the above command will start the service and keep it on even after reboots.

Similarly you can do the same for others by replacing the TSM-SSH with their respective key. ex: To keep the esxi shell open and on even after reboots

get-vmhost | Get-VMHostService | where Key -EQ TSM | Start-VMHostService | Set-VMHostService -Policy On

Now you just saved 6-7 clicks per host ;) . have fun.
You may want to suppress the warning from the esxi hosts about these services being turned on. I have cooked up a script to do the same. It has the following options.
I have also enabled it to make it possible to target certain clusters or all. Just replace the value * for $cluster with the name of the cluster to perform it on a specific cluster.

Write-Host "
1. Enable SSH
2. Enable SSH+SSH Policy ON
3. Disable SSH+SSH Policy OFF
4. Enable Shell
5. Enable Shell+Shell Policy ON
6. Disable Shell+Shell Policy OFF
7. Disable SSH/Shell warning
8. Reenable SSH/Shell warning
" -ForegroundColor Yellow
Write-Host "choose one of the above" -ForegroundColor Yellow
$option  = Read-Host ""
$cluster = "*"

 access the same from my github page where i try to keep it updated as and when i can.