Write a PS script to check system status.

Hello,

I need assistance to write a powershell command to read a txt/csv file looks like the following:

systemA     192.168.1.10
systemB     192.168.1.22
systemC     192.168.5.29......

and ping the IP of each system and return if they are alive:

systemA     192.168.1.10     alive
systemB     192.168.1.22     alive
systemC     192.168.5.29     not pingable......

Thanks.
nav2567Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Phil DavidsonCommented:
You could create a file like this named tester.ps1:

$Computers = Get-Content C:\list.txt

Test-Connection $Computers | Out-File C:\testResults.csv

Open in new window


I think this will do what you want.
nav2567Author Commented:
Thanks.

But I do not see the above PS tests the IP address(es) instead of the computer names.
Phil DavidsonCommented:
Try this:

$Computers = (Get-Content C:\list.txt)[1]

Test-Connection $Computers | Out-File C:\testResults.csv

Open in new window

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Revised:
Try this: See attached file for the format. Fill in first and second columns and leave third column blank.

# Author: Raheman Mohammed Abdul

# Import .CSV file with the following format

$file = Import-Csv C:\temp\SystemIP.csv
$file | % { $IP=$($_.IP) ; $_.status = if((Get-WmiObject win32_Pingstatus -Filter "Address='$IP'").statusCode -eq 0) { "Alive" } else { "Not Pingable" } }
$file | export-csv c:\temp\systemip.csv -Force -NoTypeInformation

Open in new window

SystemIP.csv
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
My script revised, see above
nav2567Author Commented:
Raheman, when I run your script, the result file has everything in one column:

systemA192.168.1.10
systemB192.168.1.22
systemC192.168.5.29...

Phil, your script returns error
Bob LearnedCommented:
I came up with this simple example, that may or may not meet your requirements:

1) Converted to CSV file with valid IP addresses, so I could use Import-Csv

SystemIP.txt

Name,IP
systemA,192.168.1.1
systemB,192.168.1.106

2) Script:

$ComputerList = Import-Csv C:\temp\SystemIP.txt

Write-Host "Computers:"

Foreach ($Computer in $ComputerList)
{
     Write-Host $Computer.Name
     Test-Connection $Computer.IP
}
nav2567Author Commented:
I am getting this error

Test-Connection : Cannot validate argument on parameter 'ComputerName'. The arg
ument is null or empty. Supply an argument that is not null or empty and then t
ry the command again.
At C:\ed\pingcheck\pingcheck.ps1:8 char:22
+       Test-Connection <<<<  $Computer.IP
    + CategoryInfo          : InvalidData: (:) [Test-Connection], ParameterBin
   dingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power
   Shell.Commands.TestConnectionCommand
Bob LearnedCommented:
1) When did you get that error?

2) What version of Powershell are you using?

I am using 4.0 with Windows 8.1

You can check version with the $PSVersionTable.PSVersion command.
nav2567Author Commented:
I am using a Windows 2008 server.
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Rename the file to SystemIP.csv
Bob LearnedCommented:
I didn't have any error with the code that I created, but I have a later version of PowerShell.  Did you check the version?  It might be the fact that a lower version of PowerShell would expect the file name to be .csv as explained.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nav2567Author Commented:
Thanks, everyone.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.