How to output to CSV file only with the offline PC hostname and with the PC owner and PC status showing as failed on 3 columns on CSV file?

shaider007 101 Reputation points
2023-04-19T14:16:15.2+00:00

Hi all,

Need some advice please. I have below script that I use, it pings all the PC hostnames listed on Hostnames.txt Fyi, I only type the PC hostname on the .txt file, I didn't include the PC user owner, if I want to Output it with the PC owner, how can I do it please? Add the PC hostname and PC owner written side by side on .txt file? Then it will output to a CSV File with correct columns?

The problem is, it is writing all the PC hostnames to a CSV file for both Online and Offline. What I need it to output are those only that are Offline PC host names with PC owners and Showing Status as PC Down.

How can I make it to write 3 columns instead?
example:

| Hostname | PC Owner | PC Status

The current output from CSV file is showing below in 1 column only:

PC123456 is Offline
PC789012 is Online

Thanks

$names=Get-content "C:\PowerShell\Hostnames.txt"
foreach($names in $names){
if(Test-Connection -ComputerName $names -Count 1 -ErrorAction SilentlyContinue){
Write-Host "$names is up" -ForegroundColor Green -BackgroundColor Blue
$output+="$names is up,"+"`n"
}
else{
Write-Host "$names is down" -ForegroundColor Red -BackgroundColor Yellow
$output+="$names is down,"+"`n"
}
}
$output | Out-File "C:\PowerShell\Result.csv"
Start-Sleep -s 15

Windows for business Windows Server User experience PowerShell
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2023-05-28T15:36:20.3866667+00:00

    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.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-04-20T18:07:39.7266667+00:00

    Once you have the managedBy property of the Computer objects populated, this should do what you asked:

    Get-Content "C:\PowerShell\Hostnames.txt" |
        ForEach-Object{
            try{
                $owner = Get-ADUser ((Get-ADComputer $_ -properties managedBy -ErrorAction STOP).managedBy)
                $owner = $owner.sAMAccountname
            }
            Catch{
                $owner = "Unknown"
            }
            if (Test-Connection -ComputerName $_ -Count 1 -Quiet -ErrorAction SilentlyContinue) {
                Write-Host "$_ is up, owner is $owner" -ForegroundColor Green -BackgroundColor Blue
                [PSCustomObject]@{
                    Host = $_
                    'PC Owner' = $owner
                    'PC Status' = 'UP'
                }
            }
            else {
                Write-Host "$_ is down, owner is $owner" -ForegroundColor Red -BackgroundColor Yellow
                [PSCustomObject]@{
                    Host = $_
                    'PC Owner' = $owner
                    'PC Status' = 'DOWN'
                }
            }
        } | Export-Csv "C:\PowerShell\Result.csv" -NoTypeInformation
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.