Translate

Wednesday, November 27, 2013

Start/Stop SharePoint Services using Powershell

Are you supporting SharePoint servers ? and you have multiple front end servers ? Do you need to stop the SharePoint services and restart them ? Below are the two powershell scripts which will do it for you. You don't have to go to services.msc identify the services and manually stop and restart them. Just use the scripts below. Cheers !

** If you dont want to stop/start the world wide services please remove W3SVC.

Stop Services

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

function StopServices(){
$services = @("SPAdminV4","SPTimerV4", "SPTraceV4","SPUserCodeV4","SPWriterV4","SPSearch4","W3SVC","OSearch14");

#Stop Windows Services
foreach($service in $services){
Stop-Service $service
$stoppedService = Get-Service $service
if($stoppedService.Status -eq "Stopped"){
Write-Message $service
}
}
}

function StopWebAnalytics(){
Get-SPServiceInstance |? {
if($_.TypeName -like "Web Analytics Data Processing Service"){
$_.UnProvision()
Write-Message "Web Analytics Data Processing Service"
}
}

Get-SPServiceInstance |? {
if($_.TypeName -like "Web Analytics Web Service"){
$_.UnProvision()
Write-Message "Web Analytics Web Service"
}
}
}

function StopIIS(){
Write-host "Stopping IIS";
iisreset /stop
}

function Write-Message([string]$serviceName){
Write-Host $number + "Successfully stopped service $serviceName"
}

StopServices
StopWebAnalytics

StopIIS

Start Services

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

function StartServices(){
$services = @("SPAdminV4","SPTimerV4", "SPTraceV4","SPUserCodeV4","SPWriterV4","W3SVC","OSearch14");

#Start Windows Services
foreach($service in $services){

Start-Service $service
$StartedService = Get-Service $service
if($StartedService.Status -eq "Running"){
Write-Message $service
}
}
}

function StartWebAnalytics(){
Get-SPServiceInstance |? {
if($_.TypeName -like "Web Analytics Data Processing Service"){
$_.UnProvision()
Write-Message "Web Analytics Data Processing Service"
}
}

Get-SPServiceInstance |? {
if($_.TypeName -like "Web Analytics Web Service"){
$_.UnProvision()
Write-Message "Web Analytics Web Service"
}
}
}

function Write-Message([string]$serviceName){
Write-Host $number + "Successfully Started service $serviceName"
}

StartServices

StartWebAnalytics