How to start and enable services in ESXi with PowerCLI

This article uses ssh and ESXi shell as example. To manipulate a different service, insert the service. To get a list of services in ESXi

#Connect the host
Connect-VIServer -Server $ESXiHost

#Get a list of Services
Get-VMHostService

Start and enable services in a single host

#Connect the host
Connect-VIServer -Server $ESXiHost

#Turn on SSH and ESXi Shell
Get-VMHostService | Where { $_.Key -eq "TSM" -or  $_.Key -eq "TSM-SSH"} | Start-VMHostService

#Enable SSH and ESXi Shell (start and stop with host)
Get-VMHostService | Where { $_.Key -eq "TSM" -or  $_.Key -eq "TSM-SSH"} | Set-VMHostService -Policy "On"

#Check the status of SSH and ESXi Shell. Policy should be "On" and Running "True"
Get-VMHostService | Where { $_.Key -eq "TSM" -or  $_.Key -eq "TSM-SSH"} 

For all hosts in vCenter

#Connect the host
Connect-VIServer -Server $vCenter

#Turn on SSH and ESXi Shell
Get-VMHost | Get-VMHostService | Where { $_.Key -eq "TSM" -or  $_.Key -eq "TSM-SSH"} | Start-VMHostService

#Enable SSH and ESXi Shell (start and stop with host)
Get-VMHost | Get-VMHostService | Where { $_.Key -eq "TSM" -or  $_.Key -eq "TSM-SSH"} | Set-VMHostService -Policy "On"

#Check the status of SSH and ESXi Shell. Policy should be "On" and Running "True"
Get-VMHost | Get-VMHostService | Where { $_.Key -eq "TSM" -or  $_.Key -eq "TSM-SSH"} 

For a specific cluster

Get-Cluster "$cluster" | Get-VMhost ...

For a specific datacenter

Get-Datacenter "$datacenter" | Get-VMhost ...

For a list of hosts

Populate esxilist.txt with a list of hosts with their IP addresses or FQDNs and run this script.

# Add VI-toolkit #
Add-PSsnapin VMware.VimAutomation.Core
# Get new root credential
$rootPassword = Read-Host "Enter  root password" -AsSecureString
$rootCredential = new-object -typename System.Management.Automation.PSCredential -argumentlist "root",$rootPassword

Get-Content esxilist.txt | %{
        Connect-VIServer $_   -User root -Password $rootCredential.GetNetworkCredential().Password -ErrorAction SilentlyContinue -ErrorVariable ConnectError | Out-Null
        If ($ConnectError -ne $Null) {
            Write-Host "ERROR: Failed to connect to ESX server:" $_
        }
        Else {
            #Turn on SSH and ESXi Shell
            Get-VMHostService | Where { $_.Key -eq "TSM" -or  $_.Key -eq "TSM-SSH"} | Start-VMHostService

            #Enable SSH and ESXi Shell (start and stop with host)
            Get-VMHostService | Where { $_.Key -eq "TSM" -or  $_.Key -eq "TSM-SSH"} | Set-VMHostService -Policy "On"
            Disconnect-VIServer -Confirm:$False | Out-Null
        }
}

For the record, I found this script on a blog which I cannot remember. I made minor modification.

Advertisements

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