Link to home
Start Free TrialLog in
Avatar of nav2567
nav2567Flag for United States of America

asked on

Use PS Script to check computer connection status.

Hello,

I have a text file that contains list of computer hostnames.

I am using the below script to check if the host is live.  If yes, put UP next to the hostname.  Otherwise, put DOWN.

For some reason, it is not working.  Please check and advise.  Thanks.

import-module activedirectory  

$names = Get-content "hnames.txt"

foreach ($name in $names){
  if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
    Write-Host "$name,up"
  }
  else{
    Write-Host "$name,down"
  }
}
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nav2567

ASKER

Agreed, thanks.  So, will the below work?

$names = Get-content "hnames.txt"
foreach ($name in $names) {
    [PSCustomObject]@{
        Name = $name
        IsUp = Test-Connection $name -Quiet -Count 1
    }
}
$name | select-object name | export-csv -notypeinformation -path c:\host.txt
Drop Select-Object, it drop the very thing you've just added. Output is .csv rather than .txt.
$names = Get-content "hnames.txt"
foreach ($name in $names) {
    [PSCustomObject]@{
        Name = $name
        IsUp = Test-Connection $name -Quiet -Count 1
    }
}
$name | export-csv -notypeinformation -path c:\host.csv

Open in new window

If all you wanted was a list of computers where were up you can simplify that:
Get-Content hnames.txt | Where-Object { Test-Connection $_ -Quiet -Count 1 } | Out-File upHosts.txt

Open in new window

Avatar of nav2567

ASKER

Wonderful. Do you advise to list of computers where were down also?
Up to you, depends what you want to do with the list. The first example above lists state (up or down) as true or false. This seemed closest to your original requirement, but a bit more PowerShell-y.

The second simply filters out anything that's down.
Avatar of nav2567

ASKER

Chris, I run the below but nothing returns.  

import-module activedirectory  

$names = Get-content "hnames.txt"
foreach ($name in $names) {
    [PSCustomObject]@{
        Name = $name
        IsUp = Test-Connection $name -Quiet -Count 1
    }
}
$name | export-csv -notypeinformation -path "C:\hoststatus.csv"
Avatar of nav2567

ASKER

The hnames.txt file contains list of servers which are live.
Ack sorry, no that won't work. Apologies, I should have spotting that.
$names = Get-content "hnames.txt"
foreach ($name in $names) {
    [PSCustomObject]@{
        Name = $name
        IsUp = Test-Connection $name -Quiet -Count 1
    } | export-csv -notypeinformation -path "C:\hoststatus.csv"  -Append
}

Open in new window

Line 8 is wrong (I'm sure you'd catch this pretty soon).  $name has no meaning at that point.  Simplest change would be to surround the entire for each statement with array @() or subexpression $() notation, then you can pipe from that to Export-Csv (or whatever).  Or you could change to using the ForEach-Object cmdlet instead of the foreach statement.
Of course, there were a couple more posts while I was typing that up.  I was referencing the code from a few posts up.
Some kind of dodgy speed trap :)
Avatar of nav2567

ASKER

I have tried but but still nothing returns...

import-module activedirectory  

$names = Get-content "hnames.txt"
foreach ($name in $names) {
    [PSCustomObject]@{
        Name = $name
        IsUp = Test-Connection $name -Quiet -Count 1
    } | export-csv -notypeinformation -path "C:\hoststatus.csv"  -Append
}
The file should have a list of things in it now. You'll see nothing on the screen. Is the file empty?
Avatar of nav2567

ASKER

I think the script should work but I do not see the hoststatus.csv file being generated at all...
The only way I see for the hoststatus.csv file to not be created is if you encountered an error (like permission denied), and you should see that on screen.
If you're using earlier than PS 3.0, some of the syntax in the last code won't be valid.
Avatar of nav2567

ASKER

Thank you Chris!!!

Your code works.