LLI Wiki — My Digital Knowledge Base

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

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

Mastodon LLIMastodon SocialFosstodon