Users: 30k; Pages Per Session: 2.5p; Avg. Session Duration: 1:35mi
TechDevOps.com
Explore Tools
{Featuring Microsoft Technologies + Enterprise Data + Cloud Computing}



Use Powershell to Stop & Start SQL Server Service
by BF (Senior MSSQL Operations - Enterprise, Cloud, Strategy)
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"
}