Private/FormatNmapXmlSummary.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

function FormatNmapXmlSummary ($nmapRun) {

    #Parse the scanned services list
    foreach ($scanInfoItem in $nmaprun.scaninfo) {
        #In the original XML, ranges of ports are summarized, e.g., "500-522"
        #Desummarize and convert each port into an explicit object
        $nmapRunServices = foreach ($serviceItem in $($scanInfoItem.services.replace("-","..")).Split(",")) {
            if ( $serviceItem -like "*..*" ) {
                $serviceItem = invoke-expression "$serviceItem"
            }
            foreach ($service in $serviceItem) {
                [PSCustomObject][ordered]@{
                    Protocol = $scanInfoItem.protocol
                    ScanType = $scanInfoItem.type
                    Service = [int]$service
                }
            }
        }

        #Generate the run summary information
        [PSCustomObject][Ordered]@{
            PSTypeName = 'PoshNmapSummary'
            Scanner = $nmaprun.scanner
            Version = $nmaprun.version
            Arguments = $nmaprun.args
            XmlOutputVersion = $nmaprun.xmloutputversion
            ScanResult = $nmaprun.runstats.finished.exit
            StartTime = ConvertFrom-UnixTimeStamp $nmaprun.start
            FinishedTime = ConvertFrom-UnixTimeStamp $nmaprun.runstats.finished.time
            ElapsedSeconds = $nmaprun.runstats.finished.elapsed
            HostsTotal = $nmaprun.runstats.hosts.total
            HostsUp = $nmaprun.runstats.hosts.up
            HostsDown = $nmaprun.runstats.hosts.down
            VerboseLevel = $nmaprun.verbose.level
            DebugLevel = $nmaprun.verbose.level
            ServicesScanned = $nmapRunServices
            RawXML = $nmaprun
        }
    } #nmaprunservices = foreach

} #If SummaryOnly