Adding a SQL Server event alert
In this recipe, we'll see the steps for adding a SQL Server event alert.
How to do it...
Let's list the steps required to complete the task:
Open PowerShell ISE as administrator.
Import the SQLPS module and create a new SMO Server object:
#import SQL Server module Import-Module SQLPS -DisableNameChecking #replace this with your instance name $instanceName = "localhost" $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
Add the following script and run:
$jobserver = $server.JobServer #for purposes of our exercise, we will drop this #alert if it already exists $alertname = "Test Alert" $alert = $jobserver.Alerts[$alertname] #if our test alert exists, we will drop it first if($alert) { $alert.Drop() } #Alert accepts a JobServer and an alert name $alert = New-Object Microsoft.SqlServer.Management.Smo.Agent.Alert $jobserver, $alertname $alert.Severity = 10 #Raise Alert when Message contains $alert.EventDescriptionKeyword...