Under an admin ise, I’ve tried your code in two different forests and although the $Hostnames has the correct fqdn of the DCs, it appears the second foreach has an issue:
Get-EventLog : The network path was not found.
At line:3 char:5
Get-EventLog "Directory Service" -ComputerName $Hostname | Where- </pre> for each host object in the pipeline.
I’ve also tried this Get-WinEvent code in these two forests:
Sometimes it helps to see what’s going on … at least for me.
You could have checked what’s in ‘$HostNames’. That might have guided you already to the issue. OR you could have put a ‘Write-Debug’ or ‘Write-Verbose’ to the loop to show what’s used as the ‘$Hostname’
Anyway … the solution should be: Either you extract the ‘naked’ HostNames in your $HostNames like this: