info@techdevops.com | 437-991-3573 | Data Engineering Services
TechDevOps.com
Resources Tools
Experts in Microsoft SQL Server on Windows, Linux, Containers | Clusters, Always On, FCI | Migrations, Cloud, Performance



Use Powershell to Stop & Start SQL Server Service
by BF (Principal Consultant; Architecture; Engineering)
2018-06-21









Solution:


$GetService = Get-Service -Name MSSQLSERVER

If ($GetService.Status -eq "Running") {
#Stop SQL Server Agent Service
Stop-Service -Name SQLSERVERAGENT -Force

#Sleep 10s
Start-Sleep -Seconds 10

#Stop SQL Server Service
Stop-Service -Name MSSQLSERVER -Force

#Sleep 60s
Start-Sleep -Seconds 60

}

$GetService = Get-Service -Name MSSQLSERVER

If ($GetService.Status -eq "Stopped" ) {

#Start SQL Server Service
Start-Service -Name MSSQLSERVER

#Sleep 60s
Start-Sleep -Seconds 60

$GetService = Get-Service -Name MSSQLSERVER

If ($GetService.Status -eq "Running")
{

#Start SQL Agent Service
Start-Service -Name SQLSERVERAGENT

#Sleep 10s
Start-Sleep -Seconds 10
}
}


$GetService = Get-Service -Name MSSQLSERVER


If ($GetService.Status -eq "Running")
{
Send-MailMessage -From "A1-VM-SQL <ops@opsplanet.com>" -To "OPS <ops@opsplanet.com>" -Subject "A1-VM-SQL SQLServer service restart successfull." -Priority High -dno onSuccess, onFailure -SmtpServer "mail01.opsplanet.com"
}
Else
{
Send-MailMessage -From "A1-VM-SQL <ops@opsplanet.com>" -To "OPS <ops@opsplanet>" -Subject "A1-VM-SQL SQLServer service restart failed. Check status ASAP." -Priority High -dno onSuccess, onFailure -SmtpServer "mail01.opsplanet.com"
}