Stopping and Starting Services during Database Move


If you are moving your databases to another DB and there are situations where you have to stop and start some SharePoint related services from Control Panel. Doing it one by one could be painful. I have a script below that can do it for you.

You can extend it for other services like MSSQLServer etc.

function StartService($ServiceName)

{

$Service = Get-Service $ServiceName

write-host $ServiceName is $Service.Status

if($Service.Status -eq “Stopped”)

{

Start-Service $ServiceName -ErrorAction Continue

write-host $ServiceName has been started.

Start-Sleep -Seconds 5

}

else

{

write-host $ServiceName is already stopped

}

}

 

function StopService($ServiceName)

{

$Service = Get-Service $ServiceName

write-host $ServiceName is $Service.Status

if($Service.Status -eq “Running”)

{

Stop-Service $ServiceName -Force -ErrorAction Continue

write-host $ServiceName has been stopped.

Start-Sleep -Seconds 5

}

else

{

write-host $ServiceName is already stopped

}

}

 

$AllServices = @(“SPTimerV4″,”SPTraceV4″,”SPUserCodeV4″,”SPAdminV4”, “SPWriterV4”, “W3SVC”, “OSearch15”)

foreach($aService in $AllServices)

{

StartService $aService

Write-Host “All Services are Started.”

}