Cristian Dehelean
Cristian Dehelean

Reputation: 21

Active Directory ldap searches/sec

I am trying to make a script that will return the value of the ldap searches/sec counter for each domain controller from my domain when that script will run.

For some reason after interrogating 4-5 domain controllers the script hangs out, nothing is displayed anymore and Ctrl+C is not responding also, only after some good seconds.

The question to you experts is: Why is this script is hanging? Am I doing something wrong? is there another way to do this?

I will put below 2 versions of my script. The first one is just one line:

[system.directoryservices.activedirectory.domain]::GetCurrentDomain() |
  ForEach-Object {$_.DomainControllers} |
  ForEach-Object {$_.Name} |
  foreach-Object {
    Get-Counter -computer $_ -counter "\\$_\DirectoryServices(*)\LDAP Searches/sec" |
      select -ExpandProperty countersamples |
      ? {$_.path -match 'ldap searches/sec'} |
      select path,cookedvalue |
      Format-Table -AutoSize
  }

The second variant:

$domain_controllers = "C:\domain_controllers.txt"
Import-CSV $domain_controllers | ForEach-Object {
  $dc = $_.domain_controller
  Get-Counter -computer $dc -counter "\\$dc\DirectoryServices(*)\LDAP Searches/sec"
}

As an additional information: my domain contains more than 10 domain controllers.

Upvotes: 1

Views: 731

Answers (1)

Cristian Dehelean
Cristian Dehelean

Reputation: 21

I have found a code on the internet that uses background runspaces. Now my script looks something like this:

$code = {
$domain_controllers = "C:\domain_controllers.txt"
Import-CSV $domain_controllers | ForEach-Object {
$dc = $_.domain_controller
$counter = Get-Counter -computer $dc -counter "\\$dc\DirectoryServices(*)\LDAP Searches/sec" `
    | select -ExpandProperty countersamples | ? {$_.path -match 'ldap searches/sec'} `
    | select path,cookedvalue
$path = $counter.path.split("\")
$log = $path[2] + " - " + $counter.cookedvalue
$log | Out-File -Append C:\result.txt 
}
}
$newPowerShell = [PowerShell]::Create().AddScript($code)
$job = $newPowerShell.BeginInvoke()
While (-Not $job.IsCompleted) {}
$newPowerShell.Dispose()

I cannot take credit for it but it does the job in maximum 1 minute. And previous job (i let it run and forgot about it) took around 40 minutes to fully run.

I still have to work on the final look of the script and involve some email sending in it but so far so good. Thanks for the tips on this.

Upvotes: 1

Related Questions