Stopping SharePoint services on multiple servers


Today I needed to move some SharePoint databases. This needed to be done on 2 different farms, but I did not feel the need to logon to every SharePoint server in order to stop every service. So I wrote a little PowerShell script. Smile

I tested this script on multiple environments.

# Stop SharePoint Servers on Multiple Servers
  
Function Stop-Admin    {
    $SPAdminV4 = Get-Service | Where {$_.Name -eq "SPAdminV4"}
  
    If ($SPAdminV4.status -ne "Stopped")    {
        Stop-Service SPAdminV4 -WarningAction SilentlyContinue
        Write-Host -ForegroundColor Green " - Done."
    } else {
        Write-Host -ForegroundColor Gray " - Already Stopped"
    }
}
  
Function Stop-Timer    {
    $SPTimerV4 = Get-Service | Where {$_.Name -eq "SPTimerV4"}
  
    If ($SPTimerV4.status -ne "Stopped")    {
        Stop-Service SPTimerV4 -WarningAction SilentlyContinue
        Write-Host -ForegroundColor Green " - Done."
    } else {
        Write-Host -ForegroundColor Gray " - Already Stopped"
    }
}
  
Function Stop-Tracing    {
    $SPTraceV4 = Get-Service | Where {$_.Name -eq "SPTraceV4"}
  
    If ($SPTraceV4.status -ne "Stopped")    {
        Stop-Service SPTraceV4 -WarningAction SilentlyContinue
        Write-Host -ForegroundColor Green " - Done."
    } else {
        Write-Host -ForegroundColor Gray " - Already Stopped"
    }
}
  
Function Stop-UserCode    {
    $SPUserCodeV4 = Get-Service | Where {$_.Name -eq "SPUserCodeV4"}
  
    If ($SPUserCodeV4.status -ne "Stopped")    {
        Stop-Service SPUserCodeV4 -WarningAction SilentlyContinue
        Write-Host -ForegroundColor Green " - Done."
    } else {
        Write-Host -ForegroundColor Gray " - Already Stopped"
    }
}
  
Function Stop-Writer    {
    $SPWriterV4 = Get-Service | Where {$_.Name -eq "SPWriterV4"}
  
    If ($SPWriterV4.status -ne "Stopped")    {
        Stop-Service SPWriterV4 -WarningAction SilentlyContinue
        Write-Host -ForegroundColor Green " - Done."
    } else {
        Write-Host -ForegroundColor Gray " - Already Stopped"
    }
}
  
Function Stop-SPSearch    {
    $SPSearch4 = Get-Service | Where {$_.Name -eq "SPSearch4"}
  
    If ($SPSearch4.status -ne "Stopped")    {
        Stop-Service SPSearch4 -WarningAction SilentlyContinue
        Write-Host -ForegroundColor Green " - Done."
    } else {
        Write-Host -ForegroundColor Gray " - Already Stopped"
    }
}
  
Function Stop-OSearch14    {
    $osearch14 = Get-Service | Where {$_.Name -eq "OSearch14"}
  
    If ($osearch14.status -ne "Stopped")    {
        Stop-Service OSearch14 -WarningAction SilentlyContinue
        Write-Host -ForegroundColor Green " - Done."
    } else {
        Write-Host -ForegroundColor Gray " - Already Stopped"
    }
}
  
Function Stop-W3SVC    {
    $W3SVC = Get-Service | Where {$_.Name -eq "W3SVC"}
  
    If ($W3SVC.status -ne "Stopped")    {
        Stop-Service W3SVC -WarningAction SilentlyContinue
        Write-Host -ForegroundColor Green " - Done."
    } else {
        Write-Host -ForegroundColor Gray " - Already Stopped"
    }
}
  
# Specify servers
[array]$servers = "Server1","Server2","Server3","Server4"
Foreach ($server in $servers) {
    Write-Host -ForegroundColor Yellow "Stopping services..."
    Write-Host "  Stopping SharePoint 2010 Administration on server $server..." -NoNewline
        Stop-Admin $server
    Write-Host "  Stopping SharePoint 2010 Timer..." -NoNewline
        Stop-Timer $server
    Write-Host "  Stopping SharePoint 2010 Tracing..." -NoNewline
        Stop-Tracing $server
    Write-Host "  Stopping SharePoint 2010 User Code Host..." -NoNewline
        Stop-UserCode $server
    Write-Host "  Stopping SharePoint 2010 VSS Writer..." -NoNewline
        Stop-Writer $server
    Write-Host "  Stopping SharePoint Foundation Search Service..." -NoNewline
        Stop-SPSearch $server
    Write-Host "  Stopping SharePoint Search Service..." -NoNewline
        Stop-OSearch14 $server
    Write-Host "  World Wide Web Publishing Service..." -NoNewline
        Stop-W3SVC $server
}

Write-Host -ForegroundColor Green "SharePoint services has been stopped on all servers"

I’m not giving guarantees. Smile

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