Using PowerCLI to start and stop services on your ESXi host

Yes if you PowerCLI you really don’t need to connect to your ESXi host using SSH. But there is always a situation once in a while when you may want to troubleshoot things by logging to your ESXi host over a SSH connection. Following function tries to enable a specific service using PowerCLI. Again a useful trick if you a person like me who looks the command line. How to use it? Save it to your $Profile.

Of course to be able to use the function you will need to be connected to your vCenter or ESXi host.

### ---------- function: Set-VMHostServiceStatus ---------- ###
function Set-VMHostServiceStatus() {
This script allows you to start/stop a service on vSphere ESXi 4.1 and above.

A service specified will only be 'started' if is 'stopped' and 'stopped' only if it is already started.
No action will be taken if the defined service name does not exist on a ESXi host.

Action can be either 'start' or 'stop'.

Set-VMHostServiceStatus.ps1 -VMHost $VMHost -ServiceName 'TSM-SSH' -Action 'start'

Set-VMHostServiceStatus.ps1 -VMHost $VMHost -ServiceName 'TSM' -Action 'stop'

Get-VMHost | Set-VMHostServiceStatus.ps1 -ServiceName 'TSM' -Action 'stop'

Supports all services that can be modified from the vSphere Client.

The ServiceName is case-sensitive. You get the service status using Get-VMHostService cmdlet.
# get commandline parameters
param (

[string]$ServiceName = $(Read-Host -prompt "Enter the ServiceName"),

[ValidateSet("Stop","Start")][string]$Action = $(Read-Host -prompt "Enter an Action")
Process {
$svc = (Get-VMHost $VMHost | Get-VMHostService | Where-Object {$_.Key -eq $ServiceName})
$CmdletName = $Action + '-VMHostService'

if ((($CmdletName -imatch "stop") -and ($svc.Running -imatch "True")) -or
(($CmdletName -imatch "start") -and ($svc.Running -imatch "False"))){
&$CmdletName -HostService $svc -Confirm:$false
else {
Write-Host "Info: Status of Service [$ServiceName] was not changed!"

### ---------- end ---------- ###

How to use it:

Get-Help Set-VMHostServiceStatus -Examples

Example1: Start a specific service on a specifc ESXi Host

Set-VMHostServiceStatus.ps1 -VMHost $VMHost -ServiceName 'TSM-SSH' -Action 'start'

Example2: Stop a specific service on a specifc ESXi Host

Set-VMHostServiceStatus.ps1 -VMHost $VMHost -ServiceName 'TSM' -Action 'stop'

Example3: Start a specific service on all connected ESXi Hosts

Get-VMHost | Set-VMHostServiceStatus.ps1 -ServiceName 'TSM' -Action 'stop'

Leave a Reply

Fill in your details below or click an icon to log in: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s