Skip to content

Commit a437ea9

Browse files
New-DbaAgent* - Add enhanced error handling for contained AG listeners (#9925)
Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
1 parent 80ca312 commit a437ea9

8 files changed

+73
-12
lines changed

public/New-DbaAgentAlert.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,16 @@ function New-DbaAgentAlert {
178178
if ($PSCmdlet.ShouldProcess($instance, "Adding the alert $name")) {
179179
try {
180180
# Supply either a non-zero message ID, non-zero severity, non-null performance condition, or non-null WMI namespace and query.
181-
$newalert = New-Object Microsoft.SqlServer.Management.Smo.Agent.Alert($server.JobServer, $name)
181+
try {
182+
$newalert = New-Object Microsoft.SqlServer.Management.Smo.Agent.Alert($server.JobServer, $name)
183+
} catch {
184+
if ($_.Exception.Message -match "newParent") {
185+
Stop-Function -Message "Cannot create agent alert through a contained availability group listener. SQL Server Agent objects are instance-level and must be managed on the instance directly. Please connect to the primary replica instead of the listener. Use Get-DbaAvailabilityGroup to find the current primary replica." -ErrorRecord $_ -Target $instance -Continue
186+
return
187+
} else {
188+
throw
189+
}
190+
}
182191
$list = "CategoryName", "DatabaseName", "DelayBetweenResponses", "EventDescriptionKeyword", "EventSource", "JobID", "MessageID", "NotificationMessage", "PerformanceCondition", "WmiEventNamespace", "WmiEventQuery", "IncludeEventDescription", "IsEnabled", "Severity"
183192

184193
foreach ($item in $list) {

public/New-DbaAgentAlertCategory.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,16 @@ function New-DbaAgentAlertCategory {
9191
} else {
9292
if ($PSCmdlet.ShouldProcess($instance, "Adding the alert category $cat")) {
9393
try {
94-
$alertCategory = New-Object Microsoft.SqlServer.Management.Smo.Agent.AlertCategory($server.JobServer, $cat)
94+
try {
95+
$alertCategory = New-Object Microsoft.SqlServer.Management.Smo.Agent.AlertCategory($server.JobServer, $cat)
96+
} catch {
97+
if ($_.Exception.Message -match "newParent") {
98+
Stop-Function -Message "Cannot create agent alert category through a contained availability group listener. SQL Server Agent objects are instance-level and must be managed on the instance directly. Please connect to the primary replica instead of the listener. Use Get-DbaAvailabilityGroup to find the current primary replica." -ErrorRecord $_ -Target $cat -Continue
99+
return
100+
} else {
101+
throw
102+
}
103+
}
95104

96105
$alertCategory.Create()
97106

public/New-DbaAgentJob.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,11 @@ function New-DbaAgentJob {
266266
try {
267267
$currentjob = New-Object Microsoft.SqlServer.Management.Smo.Agent.Job($server.JobServer, $Job)
268268
} catch {
269-
Stop-Function -Message "Something went wrong creating the job. `n" -Target $Job -Continue -ErrorRecord $_
269+
if ($_.Exception.Message -match "newParent") {
270+
Stop-Function -Message "Cannot create agent job through a contained availability group listener. SQL Server Agent objects are instance-level and must be managed on the instance directly. Please connect to the primary replica instead of the listener. Use Get-DbaAvailabilityGroup to find the current primary replica." -ErrorRecord $_ -Target $Job -Continue
271+
} else {
272+
Stop-Function -Message "Something went wrong creating the job." -Target $Job -Continue -ErrorRecord $_
273+
}
270274
}
271275

272276
#region job options

public/New-DbaAgentJobCategory.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,16 @@ function New-DbaAgentJobCategory {
9999
} else {
100100
if ($PSCmdlet.ShouldProcess($instance, "Adding the job category $cat")) {
101101
try {
102-
$jobCategory = New-Object Microsoft.SqlServer.Management.Smo.Agent.JobCategory($server.JobServer, $cat)
102+
try {
103+
$jobCategory = New-Object Microsoft.SqlServer.Management.Smo.Agent.JobCategory($server.JobServer, $cat)
104+
} catch {
105+
if ($_.Exception.Message -match "newParent") {
106+
Stop-Function -Message "Cannot create agent job category through a contained availability group listener. SQL Server Agent objects are instance-level and must be managed on the instance directly. Please connect to the primary replica instead of the listener. Use Get-DbaAvailabilityGroup to find the current primary replica." -ErrorRecord $_ -Target $cat -Continue
107+
return
108+
} else {
109+
throw
110+
}
111+
}
103112
$jobCategory.CategoryType = $CategoryType
104113

105114
$jobCategory.Create()

public/New-DbaAgentJobStep.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,11 @@ function New-DbaAgentJobStep {
252252
# Set the job where the job steps belongs to
253253
$jobStep.Parent = $currentJob
254254
} catch {
255-
Stop-Function -Message "Something went wrong creating the job step" -Target $instance -ErrorRecord $_ -Continue
255+
if ($_.Exception.Message -match "newParent") {
256+
Stop-Function -Message "Cannot create agent job step through a contained availability group listener. SQL Server Agent objects are instance-level and must be managed on the instance directly. Please connect to the primary replica instead of the listener. Use Get-DbaAvailabilityGroup to find the current primary replica." -ErrorRecord $_ -Target $instance -Continue
257+
} else {
258+
Stop-Function -Message "Something went wrong creating the job step" -Target $instance -ErrorRecord $_ -Continue
259+
}
256260
}
257261

258262
#region job step options

public/New-DbaAgentOperator.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,16 @@ function New-DbaAgentOperator {
321321
try {
322322
$JobServer = $server.JobServer
323323
$operators = $JobServer.Operators
324-
$operators = New-Object Microsoft.SqlServer.Management.Smo.Agent.Operator( $JobServer, $Operator)
324+
try {
325+
$operators = New-Object Microsoft.SqlServer.Management.Smo.Agent.Operator( $JobServer, $Operator)
326+
} catch {
327+
if ($_.Exception.Message -match "newParent") {
328+
Stop-Function -Message "Cannot create agent operator through a contained availability group listener. SQL Server Agent objects are instance-level and must be managed on the instance directly. Please connect to the primary replica instead of the listener. Use Get-DbaAvailabilityGroup to find the current primary replica." -ErrorRecord $_ -Target $server
329+
return
330+
} else {
331+
throw
332+
}
333+
}
325334

326335
if ($EmailAddress) {
327336
$operators.EmailAddress = $EmailAddress

public/New-DbaAgentProxy.ps1

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,20 @@ function New-DbaAgentProxy {
172172

173173
if ($Pscmdlet.ShouldProcess($instance, "Adding $proxyname with the $ProxyCredential credential")) {
174174
# the new-object is stubborn and $true/$false has to be forced in
175-
switch (Test-Bound -ParameterName Disabled) {
176-
$false {
177-
$proxy = New-Object Microsoft.SqlServer.Management.Smo.Agent.ProxyAccount -ArgumentList $jobServer, $ProxyName, $ProxyCredential, $true, $Description
175+
try {
176+
switch (Test-Bound -ParameterName Disabled) {
177+
$false {
178+
$proxy = New-Object Microsoft.SqlServer.Management.Smo.Agent.ProxyAccount -ArgumentList $jobServer, $ProxyName, $ProxyCredential, $true, $Description
179+
}
180+
$true {
181+
$proxy = New-Object Microsoft.SqlServer.Management.Smo.Agent.ProxyAccount -ArgumentList $jobServer, $ProxyName, $ProxyCredential, $false, $Description
182+
}
178183
}
179-
$true {
180-
$proxy = New-Object Microsoft.SqlServer.Management.Smo.Agent.ProxyAccount -ArgumentList $jobServer, $ProxyName, $ProxyCredential, $false, $Description
184+
} catch {
185+
if ($_.Exception.Message -match "newParent") {
186+
Stop-Function -Message "Cannot create agent proxy through a contained availability group listener. SQL Server Agent objects are instance-level and must be managed on the instance directly. Please connect to the primary replica instead of the listener. Use Get-DbaAvailabilityGroup to find the current primary replica." -ErrorRecord $_ -Target $instance -Continue
187+
} else {
188+
throw
181189
}
182190
}
183191

public/New-DbaAgentSchedule.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,16 @@ function New-DbaAgentSchedule {
529529
Write-Message -Message "Adding the schedule $jobschedule on instance $instance" -Level Verbose
530530

531531
# Create the schedule
532-
$jobschedule = New-Object Microsoft.SqlServer.Management.Smo.Agent.JobSchedule($Server.JobServer, $Schedule)
532+
try {
533+
$jobschedule = New-Object Microsoft.SqlServer.Management.Smo.Agent.JobSchedule($Server.JobServer, $Schedule)
534+
} catch {
535+
if ($_.Exception.Message -match "newParent") {
536+
Stop-Function -Message "Cannot create agent schedule through a contained availability group listener. SQL Server Agent objects are instance-level and must be managed on the instance directly. Please connect to the primary replica instead of the listener. Use Get-DbaAvailabilityGroup to find the current primary replica." -ErrorRecord $_ -Target $instance -Continue
537+
return
538+
} else {
539+
throw
540+
}
541+
}
533542

534543
#region job schedule options
535544
if ($Disabled) {

0 commit comments

Comments
 (0)