Advertisement
Guest User

DfsrInfo

a guest
Jun 26th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. FUNCTION DfsrInfo
  2. {
  3.  
  4.     PARAM(
  5.         [Parameter(Mandatory = $true, Position = 0)]
  6.         [string]$ComputerName
  7.     )
  8.  
  9.     BEGIN
  10.     {
  11.         # Test if we can connect to the machine
  12.         $ComputerName     = $ComputerName.ToUpper()
  13.         $ConnectionResult = Test-Connection -ComputerName $ComputerName -Count 2 -ErrorVariable ConnectionError -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
  14.        
  15.         IF($ConnectionError){ Write-Error "Unable to communicate with $ComputerName" }
  16.         IF(-not $ConnectionError){
  17.  
  18.             # If we can talk to it, test to see if it has DFS on it
  19.             $DfsExists = Get-WmiObject -ComputerName $ComputerName -Namespace 'root\MicrosoftDFS' -Query 'SELECT * FROM DfsrReplicationGroupConfig' -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
  20.  
  21.             # Set variables accordingly
  22.             IF (-not $DfsExists){ Write-Error "Unable to locate WMI Namespace 'root\MicrosoftDFS' on $ComputerName" }
  23.             IF ($DfsExists){ $GoAhead = $true }
  24.         }
  25.     }
  26.  
  27.     PROCESS
  28.     {
  29.         # If we can talk to the machine and it has DFS on it, begin here
  30.         IF ($GoAhead -eq $true)
  31.         {
  32.             $Groups      = Get-WmiObject -ComputerName $ComputerName -Namespace 'root\MicrosoftDFS' -Query 'SELECT * FROM DfsrReplicationGroupConfig'
  33.             $Folders     = Get-WmiObject -ComputerName $ComputerName -Namespace 'root\MicrosoftDFS' -Query 'SELECT * FROM DfsrReplicatedFolderConfig'            
  34.             $Connections = Get-WmiObject -ComputerName $ComputerName -Namespace 'root\MicrosoftDFS' -Query 'SELECT * FROM DfsrConnectionConfig'
  35.  
  36.             FOREACH($ReplicationGroup in $Groups)
  37.             {
  38.                 ##
  39.                 # Create object for replication group
  40.                 $PropertiesToLocate = @(
  41.                     'To'
  42.                     'From'
  43.                     'ReplicationGroupName'
  44.                     'ReplicatedFolderName'
  45.                     'DfsrState'
  46.                     'DfsrBacklog'    
  47.                     'DfsrBacklogFiles'            
  48.                 )
  49.                 ##
  50.                 ## Create Empty Properties, for each of the Properties above
  51.                 $OutObj = New-Object PsObject
  52.                 FOREACH ($p in $PropertiesToLocate){ $OutObj | Add-Member -MemberType NoteProperty -Name $p -Value 'nullvalue' }
  53.                 ##
  54.  
  55.  
  56.  
  57.                 FOREACH($ReplicatedFolder in $Folders)
  58.                 {
  59.                     FOREACH($ReplicationConnection in $Connections)
  60.                     {
  61.                         IF (($ReplicationGroup.ReplicationGroupGuid -eq $ReplicatedFolder.ReplicationGroupGuid) -and ($ReplicationGroup.ReplicationGroupGuid -eq $ReplicationConnection.ReplicationGroupGuid))
  62.                         {
  63.                             IF(($ReplicatedFolder.Enabled) -and ($ReplicationConnection.Enabled))
  64.                             {
  65.                                 $TheQuery              =  ("SELECT * FROM DfsrReplicatedFolderInfo WHERE ReplicationGroupGUID = '" + $ReplicationGroup.ReplicationGroupGuid + "' AND ReplicatedFolderName = '" + $ReplicatedFolder.ReplicatedFolderName + "'")                              
  66.                                 $ErrorActionPreference =  'SilentlyContinue'
  67.                                                                
  68.                                 IF($ReplicationConnection.Inbound)
  69.                                 {
  70.                                     $ToMem                       = $ComputerName
  71.                                     $FromMem                     = $ReplicationConnection.PartnerDns.Trim()
  72.                                     $InbWmi                      = Get-WmiObject -ComputerName $ToMem -Namespace 'root\MicrosoftDFS' -Query $TheQuery                                    
  73.                                     $OutObj.To                   = $ToMem
  74.                                     $OutObj.From                 = $FromMem
  75.                                     $OutObj.ReplicationGroupName = $ReplicationGroup.ReplicationGroupName
  76.                                     $OutObj.ReplicatedFolderName = $ReplicatedFolder.ReplicatedFolderName
  77.                                    
  78.                                     IF ($InbWmi){
  79.                                         SWITCH($InbWmi.State)
  80.                                         {
  81.                                             '0' { $TheState = "Uninitialized" }
  82.                                             '1' { $TheState = "Initialized" }
  83.                                             '2' { $TheState = "Initial Sync" }
  84.                                             '3' { $TheState = "Auto Recovery" }
  85.                                             '4' { $TheState = "Normal" }
  86.                                             '5' { $TheState = "In Error" }                                                                          
  87.                                         }
  88.  
  89.                                         $OutObj.DfsrBacklog = $InbWmi.GetOutboundBacklogFileCount($($InbWmi.GetVersionVector().VersionVector)).BacklogFileCount
  90.                                         $OutObj.DfsrState   = $TheState
  91.  
  92.                                         IF($OutObj.DfsrBacklog -gt 0){
  93.                                             $BackLogFiles = @()
  94.                                             $InbWmi.GetOutboundBacklogFileIdRecords($($InbWmi.GetVersionVector().VersionVector)).BacklogIdRecords.FullPathName | ForEach-Object {
  95.                                                 $BackLogFiles += "$($_); "
  96.                                             }
  97.                                             $OutObj.DfsrBacklogFiles = $BackLogFiles
  98.                                         } IF($OutObj.DfsrBacklog -le 0){
  99.                                             $OutObj.DfsrBacklogfiles = '0'    
  100.                                         }
  101.  
  102.                                     } IF(-not $InbWmi){
  103.                                         $OutObj.DfsrBacklog = "Unable to query $ToMem"
  104.                                         $OutObj.DfsrState   = "Unable to query $ToMem"
  105.                                     }
  106.  
  107.                                 }
  108.  
  109.                                 IF(-not ($ReplicationConnection.Inbound))
  110.                                 {
  111.                                     $ToMem                       = $ReplicationConnection.PartnerDns.Trim()
  112.                                     $FromMem                     = $ComputerName
  113.                                     $OutWmi                      = Get-WmiObject -ComputerName $ToMem -Namespace 'root\MicrosoftDFS' -Query $TheQuery
  114.                                     $OutObj.To                   = $ToMem
  115.                                     $OutObj.From                 = $FromMem
  116.                                     $OutObj.ReplicationGroupName = $ReplicationGroup.ReplicationGroupName
  117.                                     $OutObj.ReplicatedFolderName = $ReplicatedFolder.ReplicatedFolderName
  118.                                    
  119.                                     IF ($OutWmi){
  120.                                         SWITCH($OutWmi.State)
  121.                                         {
  122.                                             '0' { $TheState = "Uninitialized" }
  123.                                             '1' { $TheState = "Initialized" }
  124.                                             '2' { $TheState = "Initial Sync" }
  125.                                             '3' { $TheState = "Auto Recovery" }
  126.                                             '4' { $TheState = "Normal" }
  127.                                             '5' { $TheState = "In Error" }                                                                          
  128.                                         }
  129.  
  130.                                         $OutObj.DfsrBacklog = $OutWmi.GetOutboundBacklogFileCount($($OutWmi.GetVersionVector().VersionVector)).BacklogFileCount
  131.                                         $OutObj.DfsrState   = $TheState
  132.  
  133.                                         IF($OutObj.DfsrBacklog -gt 0){
  134.                                             $BackLogFiles = @()
  135.                                             $InbWmi.GetOutboundBacklogFileIdRecords($($InbWmi.GetVersionVector().VersionVector)).BacklogIdRecords.FullPathName | ForEach-Object {
  136.                                                 $BackLogFiles += "$($_); "
  137.                                             }
  138.                                             $OutObj.DfsrBacklogFiles = $BackLogFiles
  139.                                         } IF($OutObj.DfsrBacklog -le 0){
  140.                                             $OutObj.DfsrBacklogfiles = '0'    
  141.                                         }
  142.  
  143.                                     } IF(-not $OutWmi){
  144.                                         $OutObj.DfsrBacklog = "Unable to query $ToMem"
  145.                                         $OutObj.DfsrState   = "Unable to query $ToMem"
  146.                                     }
  147.  
  148.                                 }
  149.  
  150.                                 $OutObj
  151.                             }
  152.                         }                
  153.                     }
  154.                 }                
  155.             }          
  156.         }
  157.     }
  158.  
  159.     END
  160.     {
  161.    
  162.     }
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement