MicrosoftPowershell

Check Active Directory Latency With Powershell

For managing large scale Active directory environment more important part checking and understanding replication quality. In this condition i used repadmin command to check replication healthy. But this command not enough to understand exactly replication finished time. So i start to search internet and find script to check active directory latency with poweshell (http://bsonposh.com/archives/276) when i started this script i understood this script not handle large scale of dc environment. and it’s enter loop if replication will not finished.

After i changed this script ;

  1. user can edit how many time to try replication;
  2. separated replication success&failure situation
  3. Replication time
  4. Average of replication

How script works first script connect ldap and query how many domain controller have in your environment; then  crate test contact under user ou start “savasTest” + (Get-Date -f MMddyyHHmmss)” and after try to connect all dc and check this contact object exist or not.

Param($target = (([ADSI]"LDAP://rootDSE").dnshostname),
      $fqdn = (([ADSI]"").distinguishedname -replace "DC=","" -replace ",","."),
      $ou = ("cn=users," + ([ADSI]"").distinguishedname),
      $remove = $true,
      [switch]$table
      )
$context = new-object System.DirectoryServices.ActiveDirectory.DirectoryContext("Domain",$fqdn)
$dclist = [System.DirectoryServices.ActiveDirectory.DomainController]::findall($context)

$name = "savasTest" + (Get-Date -f MMddyyHHmmss)
Write-Host "`n  Creating Temp Contact Object [$name] on [$target]"
$contact = ([ADSI]"LDAP://$target/$ou").Create("contact","cn=$Name")
$contact.SetInfo()
$dn = $contact.distinguishedname
Write-Host "  Temp Contact Object [$dn] Created! `n"

$start = Get-Date

Write-Host "  Found [$($dclist.count)] Domain Controllers"

$dataTable = New-Object System.Data.DataTable            
$dataTable.Columns.Add("DcName")  | Out-Null
$dataTable.Columns.Add("Success", [int]) | Out-Null 
$dataTable.Columns.Add("RepTime")  | Out-Null
foreach($dc in $dclist)
    {
             $row = $dataTable.NewRow()
             $row["DcName"] = $dc.Name
             $row["Success"] = 0
             $row["RepTime"] = "yok"
             $dataTable.Rows.Add($row)
       }

$baksinMi = $true

Write-Host "how many turn you want to try" -ForegroundColor Cyan
$trycount = Read-Host 

for($i = 0;$i -lt $trycount; $i++)
                    {
                           $oldpos = $host.UI.RawUI.CursorPosition
                           Write-Host "  =========== Check $i ===========" -fore white
                           start-Sleep 1                          

                            foreach($dc in $dclist)
                           {
                                        $baksinMi = $true

                                        for($d = 0;$d -lt $dataTable.Rows.Count; $d++)
                                        {
                                               if($dataTable.Rows[$d][0] -eq $dc.Name)
                                               {
                                                      if($dataTable.Rows[$d][1] -eq 1)
                                                      {
                                                            $baksinMi = $false
                                                      }
                                                      else
                                                      {
                                                            $baksinMi = $true
                                                      }

                                                      break;
                                               }
                                        }

                                        if($baksinMi = $true)
                                        {
                                               if($target -match $dc.Name){continue}
                            $object = [ADSI]"LDAP://$($dc.Name)/$dn"                                                                                 
                                               if($object.name)
                                               {
                                                      Write-Host "  - $($dc.Name.ToUpper()) Has Object [$dn]" (" "*5) -fore Green

													  $dataTable | where {$_.DcName -eq $dc.Name} | foreach {$_.Success = 1; $_.RepTime = ("{0:n2}" -f ((Get-Date)-$start).TotalSeconds).ToString()}
                                               }
                                               else
                                               {
                                                      Write-Host "  ! $($dc.Name.ToUpper()) Missing Object [$dn]" -fore Red;
                                               }
                                        }
                           }                                                                
                     }

$end = Get-Date
$duration = "{0:n2}" -f ($end.Subtract($start).TotalSeconds)
Write-Host "`n    Took $duration Seconds `n" -fore Yellow

if($remove)
{
    Write-Host "  Removing Test Object `n" -fore Red
    ([ADSI]"LDAP://$target/$ou").Delete("contact","cn=$Name")
}

if($dataTable) { $dataTable | Format-Table -AutoSize }

$abc=$dataTable | where {$_.Success -eq 1}|measure|Select-Object count
Write-Host "  Total $($dclist.count) Domain Controller's replicate only $($abc.count) " -ForegroundColor Cyan
$ortalama = ($abc.count*100)/$dclist.count
Write-Host " Averege %  $ortalama server replicated." -ForegroundColor Yellow

 

Hi, I’m Savaş Saygılı