2011-12-06

Restarting vCenter Services - with PowerShell

Has it ever happened that you need to restart a vCenter service? I guess that you have been there before. Once upon a time I wrote a post that mentioned that there are not enough tools available today for us to troubleshoot the vCenter service which usually ends in a restart of the vCenter service.

When you want to stop the vCenter service you will notice that there are several services that depend on the vpxd service so they also need to be stopped. Windows will prompt you for this, of course.

Dependencies Prompt

But Windows will not start these services again automatically when you start the vCenter service.

For vCenter 4.x - it is the VMware VirtualCenter Management Webservices (vctomcat) service

That was actually easy to get.

Get-Service -ComputerName vcenter.maishsk.local -Name vpxd | select -ExpandProperty DependentServices | ft -AutoSize
vCenter 4.x

vCenter 5.x there are two additional services.

vCenter 5.x

So that means when you restart a vCenter 5.0 Service then you have to restart another 3 services as well.

PowerShell again to the rescue - and the Restart-vCenterServices function

Function Restart-vCenterServices {
	$services= @() 
	$services += (Get-Service -Name vpxd).Name
	## Add the dependencies to the variable.
	(Get-Service -Name vpxd).DependentServices | ForEach-Object {
		$services += $_.Name
	}
	## First put the services in the correct order and then stop them 
	$services | Sort-Object | ForEach-Object {
		Write-Host Stopping $_
		## -Force was used because the services have dependencies - even though they are stopped
		Get-Service $_ | Stop-Service -Force
		sleep 5
	}
	sleep 5
	## We need to start the services in reverse order 
	$services | Sort-Object -Descending | ForEach-Object {
		Write-Host Starting $_
		Get-Service $_ | Start-Service
		sleep 5
	}
}

This function was written so that it would work for both versions of vCenter.

Hope you enjoyed the ride…