Requirement: Automate SQL Agent Service stop-Start at FailOver cluster level.

Stop Script in PS(FileName:StopSqlServerAgent.ps1):

$ErrorActionPreference=”SilentlyContinue”
Stop-Transcript | out-null
$ErrorActionPreference = “Continue”
Start-Transcript -path .\StopSqlServerAgentoutput.txt -append
Stop-ClusterResource “SQL Server Agent (MSGBOXDBSEC1)”
Stop-ClusterResource “SQL Server Agent (MSGBOXDBSEC2)”
Stop-ClusterResource “SQL Server Agent”
Stop-Transcript

Caller Command in .bat:

start powershell -command “& ‘.\StopSqlServerAgent.ps1’

Start Script in PS(FileName:StartSqlServerAgent.ps1):

$ErrorActionPreference=”SilentlyContinue”
Stop-Transcript | out-null
$ErrorActionPreference = “Continue”
Start-Transcript -path .\StartSqlServerAgentoutput.txt -append
Start-ClusterResource “SQL Server Agent (MSGBOXDBSEC1)”
Start-ClusterResource “SQL Server Agent (BAM)”
Start-ClusterResource “SQL Server Agent (MSGBOXDBSEC2)”
Start-ClusterResource “SQL Server Agent”
Stop-Transcript

Caller Command in .bat:

start powershell -command “& ‘.\StartSqlServerAgent.ps1’