Functions/Test-IP.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#region: Test-IP
<#
 .Synopsis
 Tests IP Connectivity on given port
 
 .Description
    Tests IP Connectivity on given port.
    Test returns $True or throws an error.
  
 .Parameter IP
    IP Address of your system to Test
   
    .Parameter Port
    Port to Test
   
 .Example
 Test-IP -IP 192.168.2.1 -Port 8443
    
 .Example
 Test-IP -IP 192.168.2.1
 
#Requires PS -Version 4.0
#>

function Test-IP {
    param (
        [parameter(Mandatory=$true, Position=0)]
            [String]$IP,
        [parameter(Mandatory=$false, Position=1)]
            [Int]$Port = 443

    )
    Process {
        try {
            Write-Verbose "Testing connectivity to $($IP) on port $($Port)"
            $TCPClient = New-Object Net.Sockets.TcpClient
            $TCPClient.Connect($IP, $Port)
            $TCPClient.Close()
            return $true
        }
        catch [Exception] {

            throw "Could not connect to $($IP) on port $($Port)"

        }
    }
}