Private/InvokeNmapExe.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
    function InvokeNmapExe ($nmapExe,$argumentList,$computerName,[int]$updateInterval=1,[Switch]$Raw) {
        [Collections.Arraylist]$nmapExeOutput = New-Object Collections.ArrayList
        if (-not $Raw) {
            $ArgumentList += "-oX","-",'--stats-every',"200ms"
        }

        write-verbose "Invoking $nmapexe $argumentList $computerName"

        & $nmapexe $argumentList $computerName | Foreach-Object {
            if ($Raw) {$PSItem} else {
                #Process task entries
                if ($PSItem -match '^<taskprogress') {
                    $taskprogress = ([xml]$PSItem).taskprogress
                    #write-debug "Task $($taskProgress.task) is $([int]($taskProgress.percent))% complete with $($taskProgress.remaining) items left"

                    $ETA = [TimeSpan]::FromSeconds($taskProgress.etc - $taskProgress.time)
                    $WriteProgressParams = @{
                        Id=10
                        Activity = "Invoke-Nmap Scan of $($computername -join ',')"
                        Status = "$($taskProgress.task)"
                        CurrentOperation = "$($taskProgress.remaining) items remaining. ETA $ETA"
                        PercentComplete = $taskProgress.percent
                    }
                    Write-Progress @WriteProgressParams
                } else {
                    Write-Verbose $PSItem
                }

                $nmapExeOutput.Add($PSItem) > $null
            }
        }
        write-progress -id 10 -Activity 'Invoke-Nmap Scan' -Completed
        return $nmapExeOutput

    }