If you populate the "managedBy" property of the AD Computer objects the earlier script will work, and your input file should contain only the name of the computer.
If, as you say now, that you can create a file the contains the name of the computer and the "owner" of that computer, in the format "<computername><space><hyphen><space><owner-name>", then this script will work:
Get-Content "C:\PowerShell\Hostnames.txt" |
ForEach-Object{
$a = $_ -split "-"
$pc = $a[0].Trim()
$owner = $a[1].trim()
if ($owner.Length -lt 1){
$owner = "Unknown"
}
if (-NOT (Test-Connection -ComputerName $pc -Count 1 -Quiet -ErrorAction SilentlyContinue)) {
Write-Host "$pc is down, owner is $owner" -ForegroundColor Red -BackgroundColor Yellow
[PSCustomObject]@{
Host = $pc
'PC Owner' = $owner
'PC Status' = 'DOWN'
}
}
} | Export-Csv "C:\PowerShell\Result.csv" -NoTypeInformation
If you're only using a DC that is local to where you run the script it will make no difference in the speed at which the script runs if you get the owner from the computers' managedBy property. What would make a difference is if you have multiple AD Domains in your forest. In that case you should using a GC instead of a DC to get the information.
EDIT: Corrected code. Used $a where I meant to use $pc.