# Pinging Variables

**URL:** <https://forums.powershell.org/t/pinging-variables/20310>\
**Category:** PowerShell Help\
**Created:** [August 30, 2022, 1:18pm UTC](https://forums.powershell.org/t/pinging-variables/20310 "2022-08-30T13:18:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hermjon48](https://avatars.discourse-cdn.com/v4/letter/h/b9e5f3/32.png) [@Hermjon48](https://forums.powershell.org/u/Hermjon48)\
**Post date:** [August 30, 2022, 1:18pm UTC](https://forums.powershell.org/t/pinging-variables/20310/1 "2022-08-30T13:18:45Z")

</div>

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

* * *

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 30, 2022, 2:09pm UTC](https://forums.powershell.org/t/pinging-variables/20310/2 "2022-08-30T14:09:00Z")

</div>

Herman,  
Welcome to the forum. 👋🏽

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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

---

<div class="post-metadata">

**Author:** ![Hermjon48](https://avatars.discourse-cdn.com/v4/letter/h/b9e5f3/32.png) [@Hermjon48](https://forums.powershell.org/u/Hermjon48)\
**Post date:** [August 30, 2022, 3:16pm UTC](https://forums.powershell.org/t/pinging-variables/20310/3 "2022-08-30T15:16:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 30, 2022, 3:42pm UTC](https://forums.powershell.org/t/pinging-variables/20310/4 "2022-08-30T15:42:01Z")

</div>

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 😉 ) 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. 😉

Try it this way:

```auto
$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.

```auto
$IpAddressList = 
'172.168.1.9',
'10.25.62.100'

Test-Connection -ComputerName $IpAddressList |
    Out-GridView

```

---

<div class="post-metadata">

**Author:** ![Hermjon48](https://avatars.discourse-cdn.com/v4/letter/h/b9e5f3/32.png) [@Hermjon48](https://forums.powershell.org/u/Hermjon48)\
**Post date:** [August 30, 2022, 4:33pm UTC](https://forums.powershell.org/t/pinging-variables/20310/5 "2022-08-30T16:33:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 30, 2022, 6:42pm UTC](https://forums.powershell.org/t/pinging-variables/20310/6 "2022-08-30T18:42:28Z")

</div>

> [@Hermjon48](#):
>
> I am trying to dumb down a network test for a remote user

A _user_ using PowerShell? I never know if that’s a good idea. 😉

> [@Hermjon48](#):
>
> 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.

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

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…

```auto
$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.

```auto
$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

```

> [@Hermjon48](#):
>
> Regarding the error,

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

---

<div class="post-metadata">

**Author:** ![Hermjon48](https://avatars.discourse-cdn.com/v4/letter/h/b9e5f3/32.png) [@Hermjon48](https://forums.powershell.org/u/Hermjon48)\
**Post date:** [August 31, 2022, 5:11am UTC](https://forums.powershell.org/t/pinging-variables/20310/7 "2022-08-31T05:11:34Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 31, 2022, 8:08am UTC](https://forums.powershell.org/t/pinging-variables/20310/8 "2022-08-31T08:08:12Z")

</div>

> [@Hermjon48](#):
>
> the code references a csv file is that something I should create within the same folder as the script?

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 …

> **[Import-Csv (Microsoft.PowerShell.Utility) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/import-csv?view=powershell-7.2)**
>
> The Import-Csv cmdlet creates table-like custom objects from the items in CSV files. Each column in the CSV file becomes a property of the custom object and the items in rows become the property values. Import-Csv works on any CSV file, including...

> [@Hermjon48](#):
>
> Also, is there a way to include an average of the ping times (average of 4)?

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? 😉

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

> **[Test-Connection (Microsoft.PowerShell.Management) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-connection?view=powershell-7.2)**
>
> The Test-Connection cmdlet sends Internet Control Message Protocol (ICMP) echo request packets, or pings, to one or more remote computers and returns the echo response replies. You can use this cmdlet to determine whether a particular computer can be...

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

> **[Measure-Object (Microsoft.PowerShell.Utility) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/measure-object?view=powershell-7.2)**
>
> The Measure-Object cmdlet calculates the property values of certain types of object. Measure-Object performs three types of measurements, depending on the parameters in the command. The Measure-Object cmdlet performs calculations on the property...

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/pinging-variables/20310/9 "2024-05-16T20:16:42Z")

</div>


