Pinging Variables

Greetings one and all, I am trying to save an IP address to a named variable then run ‘test-connection’ to return an out-gridview of the variable used to replace the IP and the average timeout of 4 pings.

Below is a sample of what I have cobbled together. Please do have patience with me as I am just beginning my PowerShell journey, thank you.


$Myname = 172.168.1.9
$yourname = 10.25.62.100

Test-Connection $Myname,$yourname | Out-GridView


Herman,
Welcome to the forum. :wave:t4:

What is your question?

And please … when you post code, sample data, console output or error messages format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Terribly sorry about the sample code above thought the instructions also said that a ticked line above and below were sufficient. I will attempt to edit it;

The sample I pasted above does not return a window of the IP addresses displayed by the variables they are saved in and it does not show the average of 4 pings like a traditional ping command.

Hmmm I assume you’re asking what’s wrong with your code. Don’t you actually get error messages? At least I do when I run your code as it is (of course with changed IP addresses :wink: ) If you get error messages you should share them here completely formatted as code. They are an important language feature telling you what’s wrong and sometimes even what to change. :wink:

Try it this way:

$Myname = '172.168.1.9'
$yourname = '10.25.62.100'

Test-Connection -ComputerName $Myname, $yourname |
    Out-GridView

Actually you don’t need two separate variables. You can use an array.

$IpAddressList = 
'172.168.1.9',
'10.25.62.100'

Test-Connection -ComputerName $IpAddressList |
    Out-GridView

Yes you have the gist of it, but I am clear on the syntax the issue is I want the out-gridview to return the original name I assigned to it.

Instead of resolving the name using DNS. The idea is, I am trying to dumb down a network test for a remote user and if the out put reads an IP address they get confused, so I need to be able to personalize it to a name that is known to them like, say for instance “Branch Office”

I am not sure if I a making sense. Your example above saved those IPs in the variable called “IpAddressList” but does not return an output with the names of the variables.

Regarding the error, I have been able to save the IP addresses to the variables I named and subsequently have the test-connection ping the name of the variables as opposed to the IP address. My issue is the output always reverts to the IP or DNS name. So its not so much an error as much as it is a lack of knowledge on my part.

A user using PowerShell? I never know if that’s a good idea. :wink:

If they get confused by an IP address will a network test make sence for them? :wink:

So you should not use the pure output of the cmdlet. Instead you should have a source where you store the IP address including the according name you wish to use. The easiest format would be CSV. Something like this…

$InputData = @'
BranchOffice,IPAddress
"Oslo","172.168.1.9"
"Stockholm","10.25.62.100"
"Copenhagen","192.168.23.35"
'@ |
ConvertFrom-Csv

Now you can use this data and enrich it with the result of the Test-Connection command.

$Result = 
foreach ($Element in $InputData) {
    $Connection = Test-Connection -ComputerName $Element.IPAddress -Quiet -Count 1
    [PSCustomObject]@{
        BranchOffice   = $Element.BranchOffice
        IPAddress      = $Element.IPAddress
        ConnectionTest = if ($Connection) { 'passed' }else { 'failed' }
    }
}

$Result |
    Out-GridView

What I actually meant was if the code you posted runs just like this. I would have expected that it throws errors.

Wow that code looks a lot better, but a few additional questions the code references a csv file is that something I should create within the same folder as the script? Also, is there a way to include an average of the ping times (average of 4)?

You have gotten me further in less than a few minutes of your time, than I have gotten in the past 2 months. Thank you…

That’s totally up to you. You can specify a full path or a relative path for the import command. You should always read the help for the cmdlets you’re about to use completely including the examples to learn how to use them. You may start with …

Of course there is. You can do whatever you think you need. But that would need a little more complexity in the code. Would that be worth the effort? :wink:

Please read the help for the following cmdlets you could use for this task

!! the output of Test-Connection depends heavily on the version of PowerShell. There’s a distinct difference between version 5.1 and 7.x !!