Restart Sql Server Agent Service with SMO
# Date: 23/02/13
# Author: John Sansom
# Description: PS script to restart the SQL Server Agent Service for the provided instance
#
# Version: 1.0
#
# Example Execution: .\Restart_SQLServerAgent.ps1 ServerName
param([String]$ServerName)
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | Out-Null
#Create a new Managed computer object for the instance
$mc = new-object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer $ServerName
$sqlagnt = $mc.Services['SQLSERVERAGENT']
Write-Host "Stopping SQL Server Agent"
$sqlagnt.Stop()
start-sleep -s 10
$sqlagnt.Start()
Write-Host "Started SQL Server Agent"