-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSyncSSRS.ps1
70 lines (51 loc) · 2.75 KB
/
SyncSSRS.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function Sync-SSRSSubscriptionJobs {
<#
.SYNOPSIS
It validates and removes SSRS Subscription Jobs from Secondary Servers
.DESCRIPTION
It validates and removes SSRS Subscription Jobs from Secondary Servers
.PARAMETER SqlInstance
Listener Name representing the SQL Server to connect to. This can be a collection of listeners, usually pointing to different environments
.NOTES
Tags: SSRS, Subscriptions
Author: Marcos Freccia
Website: http://marcosfreccia.wordpress.com
License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
.EXAMPLE
Sync-SSRSSubscriptionJobs -SqlInstance MyListener001
It compares and syncs the SSRS Subscription Jobs in the primary and secondary replicas of MyListener001.
#>
[cmdletbinding(DefaultParametersetName = 'None')]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)][object[]]$SQLInstance,
[Parameter(Position = 1, Mandatory = $false)][switch]$Report
)
try {
foreach ($SQL in $SQLInstance) {
# The Failover always happens from the secondary initiating. So this command returns the secondary server at the moment.
$SecondaryReplica = Get-DbaAgReplica -SqlInstance $SQL | Where-Object {$_.Role -eq "Secondary" } | Select-Object -Expand Name
$SSRSJobs = (Invoke-Sqlcmd2 -ServerInstance $SecondaryReplica -Database msdb -Query "SELECT job.name FROM dbo.sysjobs AS job
JOIN dbo.syscategories AS cat ON cat.category_id = job.category_id WHERE cat.name IN ('Report Server')")
if ($SSRSJobs.Count) {
Write-Host "Number of jobs found in the ${SecondaryReplica}:"$SSRSJobs.Count -ForegroundColor Yellow
foreach ($SSRSJob in $SSRSJobs.name) {
if ($Report) {
Write-Host "Job [$SSRSJob] removed successfully from $SecondaryReplica" -ForegroundColor Green
}
else {
Remove-DbaAgentJob -SqlInstance $SecondaryReplica -Job $SSRSJob -KeepHistory
Write-Host "Job [$SSRSJob] removed successfully from $SecondaryReplica" -ForegroundColor Green
}
}
}
else {
Write-Host "There are no jobs to be removed from the current secondary $SecondaryReplica" -ForegroundColor Green
}
}
}
catch {
Write-Host -ForegroundColor Red $Error[0].Exception
}
}
#Sync-SSRSSubscriptionJobs -SQLInstance contoso-listener -Report