# Define a parameter for specifying replication groups (default is an empty array) Param ( [String[]]$ReplicationGroupList = ("") ) # Get all DFS Replication Group configurations using WMI $RGroups = Get-WmiObject -Namespace "root\MicrosoftDFS" -Query "SELECT * FROM DfsrReplicationGroupConfig" # If replication groups are specified, filter the list based on the provided group names if ($ReplicationGroupList) { $SelectedRGroups = @() foreach ($ReplicationGroup IN $ReplicationGroupList) { $SelectedRGroups += $RGroups | Where-Object { $_.ReplicationGroupName -eq $ReplicationGroup } } # If no matching groups are found, exit with an UNKNOWN status if ($SelectedRGroups.count -eq 0) { Write-Host "UNKNOWN: None of the group names specified were found." exit 3 } else { $RGroups = $SelectedRGroups } } # Initialize counters for success, warning, and error $Succ = 0 $Warn = 0 $Err = 0 # Initialize an array to store output messages $Output = @() # Iterate through each DFS Replication Group foreach ($Group in $RGroups) { # Get DFS Replicated Folder configurations for the current group $RGFoldersWMIQ = "SELECT * FROM DfsrReplicatedFolderConfig WHERE ReplicationGroupGUID='" + $Group.ReplicationGroupGUID + "'" $RGFolders = Get-WmiObject -Namespace "root\MicrosoftDFS" -Query $RGFoldersWMIQ # Get DFS Replication Connection configurations for the current group $RGConnectionsWMIQ = "SELECT * FROM DfsrConnectionConfig WHERE ReplicationGroupGUID='" + $Group.ReplicationGroupGUID + "'" $RGConnections = Get-WmiObject -Namespace "root\MicrosoftDFS" -Query $RGConnectionsWMIQ # Iterate through each DFS Replication Connection for the current group foreach ($Connection in $RGConnections) { $ConnectionName = $Connection.PartnerName # Check if the connection is enabled if ($Connection.Enabled -eq $True) { # Iterate through each DFS Replicated Folder for the current connection foreach ($Folder in $RGFolders) { $RGName = $Group.ReplicationGroupName $RFName = $Folder.ReplicatedFolderName # Build and execute dfsrdiag command to get backlog information $BLCommand = "dfsrdiag Backlog /RGName:'" + $RGName + "' /RFName:'" + $RFName + "' /SendingMember:" + $ConnectionName + " /ReceivingMember:" + $env:ComputerName $Backlog = Invoke-Expression -Command $BLCommand $BacklogFileCount = 0 # Parse the dfsrdiag output to get the backlog file count foreach ($item in $Backlog) { if ($item -ilike "*Backlog File count*") { $BacklogFileCount = [int]$Item.Split(":")[1].Trim() } } # Based on the backlog file count, generate output messages and update counters if ($BacklogFileCount -eq 0) { $Output += "OK: $BacklogFileCount files in backlog $ConnectionName->$env:ComputerName for $RGName" $Succ++ } elseif ($BacklogFileCount -lt 10) { $Output += "WARNING: $BacklogFileCount files in backlog $ConnectionName->$env:ComputerName for $RGName" $Warn++ } else { $Output += "CRITICAL: $BacklogFileCount files in backlog $ConnectionName->$env:ComputerName for $RGName" $Err++ } } } } } # Based on the counts of success, warning, and error, generate the final status and exit with the appropriate code if ($Err -gt 0) { Write-Host "CRITICAL: $Err errors, $Warn warnings, and $Succ successful replications." Write-Host "$Output" exit 2 } elseif ($Warn -gt 0) { Write-Host "WARNING: $Warn warnings, and $Succ successful replications." Write-Host "$Output" exit 1 } else { Write-Host "OK: $Succ successful replications." Write-Host "$Output" exit 0 }