Public/Get-ADSIReplicaInfo.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
function Get-ADSIReplicaInfo
{
    <#
.SYNOPSIS
    Get-ADSIReplicaInfo retrieves Active Directory replication information
 
.DESCRIPTION
 
      Get-ADSIReplicaInfo connects to an Active Directory Domain Controller and retrieves Active Directory replication information
      such as latency of replication and replication status.
      If no switches are used, latency information is returned.
       
.PARAMETER ComputerName
    Defines the remote computer to connect to.
    If ComputerName and Domain are not used, Get-ADSIReplicaInfo will attempt at connecting to the Active Directory using information
    stored in environment variables.
 
.PARAMETER Domain
    Defines the domain to connect to. If Domain is used, Get-ADSIReplicaInfo will find a domain controller to connect to.
    This parameter is ignored if ComputerName is used.
 
.PARAMETER Credential
    Defines alternate credentials to use. Use Get-Credential to create proper credentials.
 
.PARAMETER NamingContext
    Defines for which naming context replication information is to be displayed: All, Configuration, Schema, Domain. The default is Domain.
 
.PARAMETER Neighbors
    Displays replication partners for the current Domain Controller.
 
.PARAMETER Latency
    Organizes replication latency information by groups, such as Hour, Day, Week, Month, TooLong, Other
 
.PARAMETER Cursors
    Displays replication cursors for the current Domain Controller.
 
.PARAMETER DisplayDC
    Displays additional information about the currently connected Domain Controller.
 
.PARAMETER FormatTable
    Formats the output as a auto-sized table and rearranges elements according to relevance.
 
    Get-ADSIReplicaInfo -Latency -FormatTable
 
    Hour Day Week Month TooLong Other
    ---- --- ---- ----- ------- -----
    {DC1.ad.local, DC2.ad.local} {} {} {} {} {}
 
.EXAMPLE
      Get-ADSIReplicaInfo
 
      Tries to find a domain to connect to and if it succeeds, it will find a domain controller to retrieve replication information.
 
.EXAMPLE
      Get-ADSIReplicaInfo -ComputerName dc1.ad.local -Credential $Credential
 
      Connects to remote domain controller dc1.ad.local using alternate credentials.
 
.EXAMPLE
      Get-ADSIReplicaInfo -Domain ad.local
 
      Connects to remote domain controller dc1.ad.local using current credentials.
 
.EXAMPLE
      Get-ADSIReplicaInfo -Domain ad.local
 
      Connects to remote domain controller dc1.ad.local using current credentials.
 
 
.NOTES
    Filename : Get-ADSIReplicaInfo.ps1
    Author : Micky Balladelli micky@balladelli.com
 
.LINK
    https://balladelli.com
#>

    [CmdletBinding()]
    param ([string]$ComputerName = $null,
        
        [string]$Domain = $null,
        
        [Alias("RunAs")]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = [System.Management.Automation.PSCredential]::Empty,
        
        [ValidateSet("Schema", "Configuration", "Domain", "All")]
        [String]$NamingContext = "Domain",
        
        [Switch]$Neighbors,
        
        [Switch]$Latency,
        
        [Switch]$Cursors,
        
        [Switch]$Errors,
        
        [Switch]$DisplayDC,
        
        [Switch]$FormatTable
    )
    
    
    # Try to determine how to connect to the remote DC.
    # A few possibilities:
    # A computername was provided
    # A domain name was provided
    # None of the above was provided, so try with either USERDNSDOMAIN or LOGONSERVER
    # Use alternate credentials if provided
    if ($ComputerName)
    {
        if ($Credential)
        {
            $context = new-object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList "DirectoryServer", $ComputerName, $Credential.UserName, $Credential.GetNetworkCredential().Password
        }
        else
        {
            $context = new-object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList "DirectoryServer", $ComputerName
        }
    }
    elseif ($domain)
    {
        if ($Credential)
        {
            $context = new-object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList "Domain", $domain, $Credential.UserName, $Credential.GetNetworkCredential().Password
        }
        else
        {
            $context = new-object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList "Domain", $domain
        }
    }
    elseif ($env:USERDNSDOMAIN)
    {
        if ($Credential)
        {
            $context = new-object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList "Domain", $env:USERDNSDOMAIN, $Credential.UserName, $Credential.GetNetworkCredential().Password
        }
        else
        {
            $context = new-object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList "Domain", $env:USERDNSDOMAIN
        }
    }
    elseif ($env:LOGONSERVER -ne '\\MicrosoftAccount')
    {
        $logonserver = $env:LOGONSERVER.replace('\\', '')
        if ($Credential)
        {
            $context = new-object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList "DirectoryServer", $logonserver, $Credential.UserName, $Credential.GetNetworkCredential().Password
        }
        else
        {
            $context = new-object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList "DirectoryServer", $logonserver
        }
    }
    else
    {
        Write-Error -Message "Could not determine where to connect to"
        return
    }
    
    # If none of switches are present, default to at least one, so we have something to show
    if (!$Latency.IsPresent -and !$Neighbors.IsPresent -and !$Errors.IsPresent -and !$Cursors.IsPresent)
    {
        [switch]$Latency = $true
    }
    
    # Determine which DC to use depending on the context type.
    # If the context is Directory Server, simply get the provided domain controller,
    # if the context is a domain, then find a DC.
    switch ($context.ContextType)
    {
        "DirectoryServer"{ $dc = [System.DirectoryServices.ActiveDirectory.DomainController]::GetDomainController($context) }
        "Domain" { $dc = [System.DirectoryServices.ActiveDirectory.DomainController]::FindOne($context) }
        default { return }
    }
    
    if ($dc)
    {
        if ($DisplayDC.IsPresent)
        {
            Write-Verbose -Message "Information about $($dc.Name)"
            $dc
        }
        $domainDN = ""
        $obj = $domain.Replace(',', '\,').Split('/')
        $obj[0].split(".") | ForEach-Object { $domainDN += ",DC=" + $_ }
        $domainDN = $domainDN.Substring(1)
        
        if ($Cursors.IsPresent)
        {
            foreach ($partition in $dc.Partitions)
            {
                if ($NamingContext -eq "All" -or
                ($NamingContext -eq "Domain" -and $partition -eq $domainDN) -or
                ($NamingContext -eq "Schema" -and $partition.Contains("Schema")) -or
                ($NamingContext -eq "Configuration" -and $partition.split(",")[0].Contains("Configuration"))
                )
                {
                    Write-Verbose -Message "Replication cursors for partition $partition on $($dc.Name)"
                    
                    $dc.GetReplicationCursors($partition) | ForEach-Object { $_ }
                    
                }
            }
        }
        if ($Latency.IsPresent)
        {
            foreach ($partition in $dc.Partitions)
            {
                if ($NamingContext -eq "All" -or
                ($NamingContext -eq "Domain" -and $partition -eq $domainDN) -or
                ($NamingContext -eq "Schema" -and $partition.Contains("Schema")) -or
                ($NamingContext -eq "Configuration" -and $partition.split(",")[0].Contains("Configuration"))
                )
                {
                    Write-Verbose -Message "Replication latency for partition $partition on $($dc.Name)"
                    
                    $cursorsArray = $dc.GetReplicationCursors($partition)
                    $sortedCursors = $cursorsArray | Sort-Object -Descending -Property LastSuccessfulSyncTime
                    
                    $hour = @()
                    $day = @()
                    $week = @()
                    $month = @()
                    $tooLong = @()
                    $other = @()
                    
                    foreach ($cursor in $sortedCursors)
                    {
                        $timespan = New-TimeSpan -Start $cursor.LastSuccessfulSyncTime -End $(Get-Date)
                        
                        if ($timespan)
                        {
                            if ($timespan.Days -eq 0 -and $timespan.Hours -eq 0)
                            {
                                $hour += $cursor.SourceServer
                            }
                            elseif ($timespan.Days -eq 0 -and $timespan.Hours -ge 1)
                            {
                                $day += $cursor.SourceServer
                            }
                            elseif ($timespan.Days -lt 7)
                            {
                                $week += $cursor.SourceServer
                            }
                            elseif ($timespan.Days -le 30 -and $timespan.Days -gt 7)
                            {
                                $month += $cursor.SourceServer
                            }
                            else
                            {
                                $tooLong += $cursor.SourceServer
                            }
                        }
                        else
                        {
                            # no timestamp we might have a Windows 2000 server here
                            $other += $cursor.SourceServer
                        }
                    }
                    
                    $latencyObject = New-Object -TypeName PsCustomObject -Property @{
                        Hour = $hour;
                        Day = $day;
                        Week = $week;
                        Month = $month;
                        TooLong = $tooLong;
                        Other = $other
                    }
                    if ($FormatTable.IsPresent)
                    {
                        $latencyObject | Select-Object -Property Hour, Day, Week, Month, TooLong, Other | Format-Table -AutoSize
                    }
                    else
                    {
                        $latencyObject
                    }
                }
            }
        }
        
        if ($Neighbors.IsPresent -or $Errors.IsPresent)
        {
            $replicationNeighbors = $dc.GetAllReplicationNeighbors()
            
            foreach ($neighbor in $replicationNeighbors)
            {
                if ($NamingContext -eq "All" -or
                ($NamingContext -eq "Domain" -and $neighbor.PartitionName -eq $domainDN) -or
                ($NamingContext -eq "Schema" -and $neighbor.PartitionName.Contains("Schema")) -or
                ($NamingContext -eq "Configuration" -and $neighbor.PartitionName.split(",")[0].Contains("Configuration"))
                )
                {
                    Write-Verbose -Message "Replication neighbors for partition $($neighbor.PartitionName) on $($dc.Name)"
                    
                    if (($Errors.IsPresent -and $neighbor.LastSyncResult -ne 0) -or $Neighbors.IsPresent)
                    {
                        if ($FormatTable.IsPresent)
                        {
                            $neighbor | Select-Object SourceServer, LastSyncMessage, LastAttemptedSync, LastSuccessfulSync, PartitionName | Format-Table -AutoSize
                        }
                        else
                        {
                            $neighbor
                        }
                    }
                }
            }
        }
    }
}