Source/Private/Import-PortData.ps1

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
47
48
49
Function Import-PortData {
    [CmdletBinding()]
    Param()
        
    $PortURL = "https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.csv"
    $File =  $env:TEMP+'\Ports.csv'
    $ErrorActionPreference = 'continue'
    $WebClient = New-Object System.Net.WebClient
    
    Try{
        $WebClient.DownloadFile($PortURL, $File)
        $data = Import-Csv $File
        If($data){
            Foreach($item in $data.where({$_.'Port Number'})){
    
                If($item.'Port number' -like "*-*"){
                    [int]$start, [int]$end = ($item.'Port number' -split "-")
                    $start..$end | ForEach-Object{
                        [PSCustomObject] @{
                            Port =  $_
                            Service = $item.'Service Name'
                            Protocol = $item.'Transport Protocol'
                            Description = $item.Description
                        }
                    }
                }
                else{
                    [PSCustomObject] @{
                        Port =  $item.'Port number'
                        Service = $item.'Service Name'
                        Protocol = $item.'Transport Protocol'
                        Description = $item.Description
                    }
                }
    
            }            
        }
    }
    catch{
        $_
    }
}

# Import-PortData -Verbose