Public/New-Networkmap.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
59
60
61
62
63
64
<#
        .SYNOPSIS
        Creates a draw.io compatible CSV file.
        .DESCRIPTION
        Creates a draw.io compatible CSV file that can be used to create a network map. The network map can be created with multiple layouts, organic is recommended for 'radial' layouts.
        Uses the file macaddress.io-db.csv to compare mac addresses and find the correct image. Also uses a guesstimate to check which device is what type.
        .PARAMETER Network
        CIDR notation of the network to scan. If no network is given it will scan all connected networks
        .EXAMPLE
        New-Networkmap -Network 192.168.15.1/24 -Layout organic -verbose
        .INPUTS
        System.String. You can pipe network values into this command
        .OUTPUTS
        CSV file
    #>

function New-Networkmap {
    [CmdletBinding()]
    Param(
        [Parameter(ParameterSetName = 'Network', Mandatory = $false)]
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [String]$Network,
        [Parameter(ParameterSetName = 'Network', Mandatory = $false)]
        [Array]$SkipIPs,
        [Parameter(Mandatory = $true)]
        [ValidateSet('auto', 'none', 'verticaltree', 'horizontaltree', 'verticalflow', 'horizontalflow', 'organic', 'circle')]$Layout
    ) 
    
    begin {
        write-host "Creating network diagram"
    }
    
    process {
        if (!$Network) { 
            $NetScan = Get-Networkscan
        }
        else {
            $NetScan = Get-Networkscan -network $Network
        }
        if ($Netscan) {
            $CSV = @"
##Network Diagram generated by PsDrawIO
# label: %hostname%
# style: shape=%shape%;fillColor=%fill%;strokeColor=%stroke%;verticalLabelPosition=bottom;aspect=fixed;
# namespace: csvimport-
# connect: {"from":"refs", "to":"id", "invert":true, "style": \
# "curved=0;endArrow=none;endFill=0;dashed=1;strokeColor=#6c8ebf;"}
# width: 40
# height: 40
# ignore: id,shape,fill,stroke,refs
# nodespacing: 20
# levelspacing: 20
# edgespacing: 20
# layout: $Layout
## CSV data starts below this line
"@

            $Scan = $($netscan | ConvertTo-Csv -Delimiter ',' -NoTypeInformation)
            return $CSV, $SCAN
        }
        else {
            Write-Warning "Could not scan network."
        }
    }
    
}