-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSyncServerSettings.ps1
84 lines (62 loc) · 4.1 KB
/
SyncServerSettings.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function Sync-SpConfigure {
<#
.SYNOPSIS
It validates and syncs all server level system configuration (sys.configuration/sp_configure) among replicas in the Availability Groups Cluster.
.DESCRIPTION
It validates and syncs all server level system configuration (sys.configuration/sp_configure) among replicas in the Availability Groups Cluster.
.PARAMETER SqlInstances
Listener Name representing the SQL Server to connect to. This can be a collection of listeners, usually pointing to different environments
.PARAMETER ConfigName
Return only specific configurations -- auto-populated from source server
.NOTES
Tags: SpConfigure, Configuration
Author: Marcos Freccia
Website: http://marcosfreccia.wordpress.com
License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
.EXAMPLE
Sync-SpConfigure -SqlInstances MyListener001 -ConfigName MaxServerMemory
It compares and syncs the MaxServerMemory setting in the primary and secondary replicas of MyListener001.
.EXAMPLE
foreach ($SpSetting in Get-DbaSpConfigure -SqlInstance MyListener001 | Select-Object -ExpandProperty ConfigName) {
Sync-SpConfigure -SQLInstances MyListener001 -ConfigName $SpSetting
}
It goes through all Server Level Settings and syncs for the primary and secondary replicas of MyListener001.
#>
[cmdletbinding(DefaultParametersetName = 'None')]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)][object[]]$SQLInstance,
[Parameter(Mandatory = $true)][string]$ConfigName,
[Parameter(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.
$PrimaryReplica = Get-DbaAgReplica -SqlInstance $SQL | Where-Object {$_.Role -eq "Primary" } | Select-Object -Expand Name
# 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
$PrimaryConfiguration = Get-DbaSpConfigure -SqlInstance $PrimaryReplica -ConfigName $ConfigName | Select-Object ConfigName, RunningValue
$SecondaryConfiguration = Get-DbaSpConfigure -SqlInstance $SecondaryReplica -ConfigName $ConfigName | Select-Object ConfigName, RunningValue
# Symbol == means that it exists in both sides
# Symbol <= means that it exists in the PrimaryNode but not in the SecondaryNode
# Symbol => means that it exists in the SecondaryNode but not in the PrimaryNode
$Results = Compare-Object -ReferenceObject $PrimaryConfiguration -DifferenceObject $SecondaryConfiguration -IncludeEqual -Property "RunningValue"
if ($Results.SideIndicator -eq "<=") {
if ($Report) {
Write-Host "The following change is going to be applied in $SecondayReplica" $ConfigName " - "$PrimaryConfiguration.RunningValue -ForegroundColor Yellow
}
else {
Set-DbaSpConfigure -SqlInstance $SecondaryReplica -ConfigName $PrimaryConfiguration.ConfigName -Value $PrimaryConfiguration.RunningValue
Write-Host "The following change is going to be applied in $SecondayReplica "$ConfigName " - "$PrimaryConfiguration.RunningValue -ForegroundColor Yellow
}
}
}
}
catch {
Write-Host -ForegroundColor Red $Error[0].Exception
}
}
$AG = "contoso-listener"
foreach ($SpSetting in Get-DbaSpConfigure -SqlInstance $AG | Select-Object -ExpandProperty ConfigName) {
Sync-SpConfigure -SQLInstance $AG -ConfigName $SpSetting -Report
}