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() {
<#
.SYNOPSIS
This script allows you to start/stop a service on vSphere ESXi 4.1 and above.

.DESCRIPTION
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'.

.EXAMPLE
$VMHost=Get-VMHost esx3.foobirds.in
Set-VMHostServiceStatus.ps1 -VMHost $VMHost -ServiceName 'TSM-SSH' -Action 'start'

.EXAMPLE
$VMHost=Get-VMHost esx3.foobirds.in
Set-VMHostServiceStatus.ps1 -VMHost $VMHost -ServiceName 'TSM' -Action 'stop'

.EXAMPLE
Get-VMHost esx3.foobirds.in | Set-VMHostServiceStatus.ps1 -ServiceName 'TSM' -Action 'stop'

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

.NOTES
The ServiceName is case-sensitive. You get the service status using Get-VMHostService cmdlet.
#>
# get commandline parameters
[CmdletBinding()]
param (
[parameter(Mandatory=$true,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True)]
[VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]$VMHost,

[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

$VMHost=Get-VMHost esx3.foobirds.in
Set-VMHostServiceStatus.ps1 -VMHost $VMHost -ServiceName 'TSM-SSH' -Action 'start'

Example2: Stop a specific service on a specifc ESXi Host

$VMHost=Get-VMHost esx3.foobirds.in
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'
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