Ordered shutdown and startup of a SharePoint farm with PowerShell
Two PowerShell scripts to stop and start the servers of a SharePoint farm in the right order (front-end, OWA, application, database).
Restarting a multi-server SharePoint farm requires a specific order: stop the front-ends first and the databases last, then start the databases first and the front-ends last. These two PowerShell scripts automate that sequence for a farm running on virtual machines.
Principle
| Tier | Servers (example) | Stop order | Start order |
|---|---|---|---|
| Front-end (web) | spfe01, spfe02 | 1 | 4 |
| Office Web Apps | spowa01 | 2 | 3 |
| Application | spapp01, spapp02 | 3 | 2 |
| Database | spdb01, spdb02 | 4 | 1 |
- Shutdown:
Stop-Computer -Forceon each server, then wait until it no longer answersTest-Connection. - Startup: start each virtual machine with a helper script (here
Start-VM.ps1, a wrapper around your hypervisor tooling such as VMware PowerCLI), then wait until the server answersTest-Connection. Wait 30 seconds after the database tier before starting the application tier. - Both scripts require the
/applyargument, to avoid running them by accident, and return exit code 0 on success and 1 on failure.
Stop script (sp_stop_server.ps1)
if ($args -notlike "/apply") {
exit
}
$db = @("spdb02","spdb01")
$app = @("spapp01","spapp02")
$owa = @("spowa01")
$wfe = @("spfe01","spfe02")
# Stopping SharePoint infrastructure...
Write-Host ""
Write-Host "Shutdown SharePoint Front-End..."
$wfe | foreach {
Write-Host ""
Write-Host "-> $_..."
Stop-Computer -ComputerName $_ -Force
do { Write-Host "." -NoNewline } while (Test-Connection $_ -Quiet)
Write-Host ""
Write-Host "-> $_ not responding..."
}
Write-Host ""
Write-Host "Shutdown OWA..."
$owa | foreach {
Write-Host ""
Write-Host "-> $_..."
Stop-Computer -ComputerName $_ -Force
}
Write-Host ""
Write-Host "Shutdown SharePoint Application..."
$app | foreach {
Write-Host ""
Write-Host "-> $_..."
Stop-Computer -ComputerName $_ -Force
do { Write-Host "." -NoNewline } while (Test-Connection $_ -Quiet)
Write-Host ""
Write-Host "-> $_ not responding..."
}
Write-Host ""
Write-Host "Shutdown SharePoint Database..."
$db | foreach {
Write-Host ""
Write-Host "-> $_..."
Stop-Computer -ComputerName $_ -Force
do { Write-Host "." -NoNewline } while (Test-Connection $_ -Quiet)
Write-Host ""
Write-Host "-> $_ not responding..."
}
$ret = $false
$db + $app + $owa + $wfe | foreach { $ret = ($ret -or (Test-Connection $_ -Count 1 -Quiet)) }
if ($ret) {
Write-Host ""
Write-Host "Not all SharePoint servers are stopped correctly!"
exit 1
} else {
Write-Host ""
Write-Host "All SharePoint servers are stopped."
exit 0
}
Start script (sp_restart_server.ps1)
if ($args.Count -eq 0) {
Write-Host ""
Write-Host "Usage: start all SharePoint infrastructure."
Write-Host " sp_restart_server.ps1 [/apply]"
Write-Host ""
exit
}
if ($args -notlike "/apply") {
exit
}
$db = @("spdb01","spdb02")
$app = @("spapp02","spapp01")
$owa = @("spowa01")
$wfe = @("spfe01","spfe02")
# Starting SharePoint infrastructure...
Write-Host ""
Write-Host "Starting SharePoint Database..."
$db | foreach {
Write-Host ""
Write-Host "-> $_..."
& .\Start-VM.ps1 $_ > Out-Null
do { Write-Host "." -NoNewline } while (!(Test-Connection $_ -Quiet -Count 1 -Delay 1))
Write-Host ""
Write-Host "-> $_ responding..."
}
Write-Host ""
Write-Host "Waiting 30 seconds..."
Start-Sleep 30
Write-Host ""
Write-Host "Starting SharePoint Application..."
$app | foreach {
Write-Host ""
Write-Host "-> $_..."
& .\Start-VM.ps1 $_ > Out-Null
do { Write-Host "." -NoNewline } while (!(Test-Connection $_ -Quiet -Count 1 -Delay 1))
Write-Host ""
Write-Host "-> $_ responding..."
}
Write-Host ""
Write-Host "Starting OWA..."
$owa | foreach {
Write-Host ""
Write-Host "-> $_..."
& .\Start-VM.ps1 $_ > Out-Null
}
Write-Host ""
Write-Host "Starting SharePoint Front-End..."
$wfe | foreach {
Write-Host ""
Write-Host "-> $_..."
& .\Start-VM.ps1 $_ > Out-Null
do { Write-Host "." -NoNewline } while (!(Test-Connection $_ -Quiet -Count 1 -Delay 1))
Write-Host ""
Write-Host "-> $_ responding..."
}
$ret = $true
$db + $app + $owa + $wfe | foreach { $ret = ($ret -and (Test-Connection $_ -Count 1 -Quiet)) }
if ($ret) {
Write-Host ""
Write-Host "All SharePoint servers are started."
exit 0
} else {
Write-Host ""
Write-Host "Not all SharePoint servers are started correctly!"
exit 1
}
Run each script with .\sp_stop_server.ps1 /apply and .\sp_restart_server.ps1 /apply.
Notes
Start-VM.ps1is not provided: replace it with your own call to the hypervisor (for exampleStart-VMfrom VMware PowerCLI).- The OWA tier is not waited on in either script (the wait loops are commented out in the original).
- The scripts need rights to shut down the remote servers and must run from a host that can reach them.