Wednesday, February 14, 2018

Testing ipv4 connecting in powershell

So we often need to test an ip while automating,be it connecting to vmware vcenter or an esxi host. you can however use the test-connection


1
2
3
4
5
6
7
8
 C:\Users\batman> test-connection 11.73.111.51

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms) 
------        -----------     -----------      -----------                              -----    -------- 
INENAMBIG1L1C 11.73.111.51                                                              32       149      
INENAMBIG1L1C 11.73.111.51                                                              32       153      
INENAMBIG1L1C 11.73.111.51                                                              32       151      
INENAMBIG1L1C 11.73.111.51                     

The problem with this is that it takes a long time and if your intention is to just get an yes or no ($true or $false) kind of answer this is a waste of your time. So I just cooked up a tiny cookie for us.


1
function test_connection_ipv4($ipv4) { if (test-connection $ipv4 -Count 1 -ErrorAction SilentlyContinue ) {$true} else {$false} }
It returns a boolean ($true or $false). If the ipv4 is good then it returns $true and else $false.