Private/Get-NetworkScan.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
50
51
52
53
54
55
56
57
58
function Get-Networkscan {
    [CmdletBinding()]
    Param(
        [Parameter(ParameterSetName = 'Network', Mandatory = $false)]
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [String]$Network
    ) 
    
    begin {
        $network = $PSBoundParameters.Network
    }
    
    process {
        if ($Network) {
            write-host "Scanning network $Network"
            get-netadapter | Remove-NetNeighbor -AddressFamily IPv4 -Confirm:$false -erroraction silentlycontinue
            $ScanResults = Invoke-PSnmap -ComputerName $network -Port 22, 445, 443, 3389, 9100 -ScanOnPingFail -DNS -NoSummary -PortConnectTimeoutMS 500
        }
        else {
            write-host "Scanning all local networks"
            $networks = Get-NetIPAddress | Where-Object -Property prefixorigin -ne "WellKnown"
            $ScanResults = foreach ($Network in $networks) {
                Invoke-PSnmap -ComputerName "$($network.ipaddress)/$($Network.prefixlength)" -Port 22, 445, 443, 3389, 9100 -ScanOnPingFail -DNS -NoSummary -PortConnectTimeoutMS 500
            }
        }
           
        $i = 0
        foreach ($Result in $ScanResults | Where-Object -Property ping -ne $false) {
            if ($Result.'ip/DNS') { $hostname = $Result.'IP/DNS' } else { $hostname = $Result.computername }
            $MACList = import-csv -path "$($MyInvocation.MyCommand.Module.ModuleBase)\private\macaddress.io-db.csv" -Delimiter ','
            $OUI = ((Get-NetNeighbor $Result.computername -erroraction silentlycontinue).linklayeraddress -replace '-', ':')
            if ($OUI) { $PossibleManafacture = $MACList | Where-Object -Property oui -eq ($OUI).substring(0, 8) }
            Write-Verbose $result.computername
            Write-Verbose "OUI Match: $PossibleManafacture"
            $DeviceType = switch ($result) {
                { $null -ne $PossibleManafacture.image } { $PossibleManafacture.image; break }
                { $_.'port 9100' -eq $true } { 'mxgraph.office.devices.printer'; break }
                { $_.'port 3389' -eq $true } { 'mxgraph.office.concepts.application_windows'; break }
                { $_.'port 445' -eq $true } { 'mxgraph.office.concepts.application_windows'; break }
                { $_.'port 443' -eq $true } { 'mxgraph.office.concepts.application_web'; break }
                { $_.'port 22' -eq $true } { 'mxgraph.office.servers.topology_builder'; break }
                Default { 'mxgraph.office.concepts.help' }
            }
            $i++
            if ($i -eq 1) { $refid = $null } else { $Refid = 1 }
            [PSCustomObject]@{
                id       = $i
                hostname = $hostname
                fill     = '#F77F00'
                stroke   = '#003049'
                shape    = $devicetype
                refs     = $RefID
            }
        }
    }
    
}