/
Get-WinADForestReplicationSummary.ps1
247 lines (222 loc) · 10.5 KB
/
Get-WinADForestReplicationSummary.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
function Get-WinADForestReplicationSummary {
<#
.SYNOPSIS
Function that retrieves the replication summary of the Active Directory forest.
.DESCRIPTION
This function retrieves the replication summary of the Active Directory forest.
It uses the repadmin command to retrieve the replication summary and then parses
the output to create a custom object with the following properties:
- Server: The server name.
- LargestDelta: The largest delta between replication cycles.
- Fails: The number of failed replication cycles.
- Total: The total number of replication cycles.
- PercentageError: The percentage of failed replication cycles.
- Type: The type of server (Source or Destination).
- ReplicationError: The replication error message.
.PARAMETER InputContent
Allow the user to pass the repadmin output as a string.
.PARAMETER FilePath
Allow the user to pass the path of a file containing the repadmin output.
.PARAMETER IncludeStatisticsVariable
Allow the user to pass the name of a variable to store the statistics.
.EXAMPLE
Get-WinADForestReplicationSummary | Format-Table
.EXAMPLE
Get-WinADForestReplicationSummary -FilePath C:\repadmin.txt | Format-Table
.EXAMPLE
Get-WinADForestReplicationSummary -InputContent $repadminOutput | Format-Table
.EXAMPLE
Get-WinADForestReplicationSummary -IncludeStatisticsVariable Statistics | Format-Table
$Statistics | Format-Table
.NOTES
General notes
#>
[CmdletBinding(DefaultParameterSetName = 'Default')]
param(
[Parameter(ParameterSetName = 'InputContent')][string] $InputContent,
[Parameter(ParameterSetName = 'FilePath')][string] $FilePath,
[string] $IncludeStatisticsVariable
)
if ($InputContent) {
$OutputRepadmin = $InputContent
} elseif ($FilePath) {
$OutputRepadmin = Get-Content -Path $FilePath -Raw
} else {
# Run repadmin and capture the output
$OutputRepadmin = repadmin /replsummary /bysrc /bydest | Out-String
}
# Split the output into sections
$sections = $OutputRepadmin -split "Source DSA|Destination DSA"
$lines = $sections[1] -split "`r`n"
[Array] $sourceData = foreach ($line in $lines) {
if ($line -match '^Experienced the following operational errors trying to retrieve replication information') {
break
}
if ($line -match '\S' -and $line -notmatch '^\s*largest') {
if ($line -match "^\s*(?<DSA>\S+)\s+(?<Rest>.*)$") {
Write-Verbose -Message "Processing line: $line"
$DSA = $Matches.DSA
# $rest = $Matches.Rest -split "\s+", 4 # split into 4 parts: LargestDelta, Fails, Total, Percentage and the rest
$Rest = $Matches.Rest
if ($rest -match ">60 days") {
$RestSplitted = $Rest -split "\s+", 7
$LargestDelta = New-TimeSpan -Days 60
$Fails = $RestSplitted[2]
$Total = $RestSplitted[4]
$Percentage = $RestSplitted[5]
$ReplicationError = $RestSplitted[6]
$Type = "Source"
} else {
$RestSplitted = $Rest -split "\s+", 4 # split into 4 parts: LargestDelta, Fails, Total, Percentage and the rest
$LargestDelta = ConvertTo-TimeSpanFromRepadmin -timeString $RestSplitted[0]
$Fails = $RestSplitted[1]
$Continue = $RestSplitted[3]
$Continue = $Continue -split "\s{2,}"
$Total = $Continue[0]
$Percentage = $Continue[1]
$ReplicationError = $Continue[2]
if ($null -eq $ReplicationError) {
$ReplicationError = "None"
}
$Type = "Source"
}
[PSCustomObject]@{
Server = $DSA
LargestDelta = $LargestDelta
Fails = if ($null -ne $Fails) { [int] $Fails.Replace("/", "").Trim() } else { $null }
Total = [int] $Total
PercentageError = $Percentage
Type = $Type
ReplicationError = $ReplicationError
}
}
}
}
$lines = $sections[2] -split "`r`n"
[Array] $destinationData = foreach ($line in $lines) {
if ($line -match '^Experienced the following operational errors trying to retrieve replication information') {
break
}
if ($line -match '\S' -and $line -notmatch '^\s*largest') {
if ($line -match "^\s*(?<DSA>\S+)\s+(?<Rest>.*)$") {
Write-Verbose -Message "Processing line: $line"
$DSA = $Matches.DSA
$Rest = $Matches.Rest
if ($rest -match ">60 days") {
$RestSplitted = $Rest -split "\s+", 7
$LargestDelta = New-TimeSpan -Days 60
$Fails = $RestSplitted[2]
$Total = $RestSplitted[4]
$Percentage = $RestSplitted[5]
$ReplicationError = $RestSplitted[6]
$Type = "Destination"
} else {
$RestSplitted = $Rest -split "\s+", 4 # split into 4 parts: LargestDelta, Fails, Total, Percentage and the rest
$LargestDelta = ConvertTo-TimeSpanFromRepadmin -timeString $RestSplitted[0]
$Fails = $RestSplitted[1]
$Continue = $RestSplitted[3]
$Continue = $Continue -split "\s{2,}"
$Total = $Continue[0]
$Percentage = $Continue[1]
$ReplicationError = $Continue[2]
if ($null -eq $ReplicationError) {
$ReplicationError = "None"
}
$Type = "Destination"
}
[PSCustomObject]@{
Server = $DSA
LargestDelta = $LargestDelta
Fails = if ($null -ne $Fails) { [int] $Fails.Replace("/", "").Trim() } else { $null }
Total = [int] $Total
PercentageError = $Percentage
Type = $Type
ReplicationError = $ReplicationError
}
}
}
}
[Array] $operationalErrors = foreach ($line in $lines) {
if ($line -match '^Experienced the following operational errors trying to retrieve replication information') {
$processingErrors = $true
continue
}
if ($processingErrors) {
if ($line -match "^\s*(?<ErrorCode>\d+)\s+-\s+(?<ServerName>.*)$") {
Write-Verbose -Message "Processing error line: $line"
$ErrorCode = $Matches.ErrorCode
$ServerName = $Matches.ServerName
if ($ServerName -match "\.") {
$HostName = $ServerName.Split(".")[0]
} else {
$HostName = $ServerName
}
[PSCustomObject]@{
Server = $HostName
LargestDelta = $null
Fails = 1
Total = 1
PercentageError = 100
Type = "Unknown"
ReplicationError = "($ErrorCode) Error trying to retrieve replication information"
}
}
}
}
# Combine the data from both sections
$ReplicationSummary = $sourceData + $destinationData + $operationalErrors
$ReplicationSummary
if ($IncludeStatisticsVariable) {
$Statistics = [ordered] @{
"Good" = 0
"Failures" = 0
"Total" = 0
"DeltaOver1Hours" = 0
"DeltaOver3Hours" = 0
"DeltaOver6Hours" = 0
"DeltaOver12Hours" = 0
"DeltaOver24Hours" = 0
"UniqueErrors" = [System.Collections.Generic.List[string]]::new()
"UniqueWarnings" = [System.Collections.Generic.List[string]]::new()
}
foreach ($Replication in $ReplicationSummary) {
$Statistics.Total++
if ($Replication.LargestDelta -gt (New-TimeSpan -Hours 24)) {
$Statistics.DeltaOver24Hours++
} elseif ($Replication.LargestDelta -gt (New-TimeSpan -Hours 12)) {
$Statistics.DeltaOver12Hours++
} elseif ($Replication.LargestDelta -gt (New-TimeSpan -Hours 6)) {
$Statistics.DeltaOver6Hours++
} elseif ($Replication.LargestDelta -gt (New-TimeSpan -Hours 3)) {
$Statistics.DeltaOver3Hours++
} elseif ($Replication.LargestDelta -gt (New-TimeSpan -Hours 1)) {
$Statistics.DeltaOver1Hours++
}
if ($Replication.Fails -eq 0) {
$Statistics.Good++
} else {
$Statistics.Failures++
}
if ($Replication.ReplicationError -notin "None", "") {
if ($Replication.ReplicationError -like "*Operational errors trying to retrieve replication information*") {
if ($Replication.ReplicationError -notin $Statistics.UniqueWarnings) {
$Statistics.UniqueWarnings.Add($Replication.ReplicationError)
}
} elseif ($Replication.ReplicationError -like "*The remote procedure call was cancelled.*") {
if ($Replication.ReplicationError -notin $Statistics.UniqueWarnings) {
$Statistics.UniqueWarnings.Add($Replication.ReplicationError)
}
} elseif ($Replication.ReplicationError -like "*The RPC server is unavailable*") {
if ($Replication.ReplicationError -notin $Statistics.UniqueWarnings) {
$Statistics.UniqueWarnings.Add($Replication.ReplicationError)
}
} elseif ($Replication.ReplicationError -notin $Statistics.UniqueErrors) {
if ($Statistics.UniqueErrors -notcontains $Replication.ReplicationError) {
$Statistics.UniqueErrors.Add($Replication.ReplicationError)
}
}
}
}
Set-Variable -Scope Global -Name $IncludeStatisticsVariable -Value $Statistics
}
}