nav2567
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"
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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.
The second simply filters out anything that's down.
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"
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"
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
}
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 :)
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
}
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?
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.
If you're using earlier than PS 3.0, some of the syntax in the last code won't be valid.
ASKER
Thank you Chris!!!
Your code works.
Your code works.
ASKER
$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