0

I'm using the Nmap-Scan.ps1 script and calling it using another script. The nmap-scan script is just a wrapper for the Nmap.exe program.

What I am required to do is provide a list of devices (by IP or DNS), run a NMAP scan, and have the results of each dropped into their own respective files.

Running the nmap-scan.ps1 directly works as expected:

.\Nmap-Scan.PS1 "localhost" -Arguments "-sS -sU -Pn -v" -OutDir "C:\Scripts\PowerShell\Get-NMAPData\Test" -Location "C:\Program Files (x86)\nmap\nmap.exe"

And I get the 'localhost.xml' file in the expected OutDir.

However, when I run the following code PowerShell shows that it is starting the jobs but I don't get any resulting XML files. So I must be doing something wrong or maybe the 'Start-Job' doesn't have the permissions to write the file out? I'm not sure either way and I'm stuck at this point. I would appreciate any help.

$MaxThreads = 2
$ServerList = Get-Content -Path "serverlist.txt"

foreach($server in $ServerList) {
    #Create hashtable with parameters and their values
    $Arg = @{
        InputObject=$server;
        Arguments="-sS -sU -Pn -v";
        OutDir="C:\Scripts\PowerShell\Get-NMAPData\Test"
    }
    Start-Job -ScriptBlock{PARAM($Arg); & C:\Scripts\PowerShell\Get-NMAPData\Nmap-Scan.PS1 @Arg} -ArgumentList $Arg

    while (@(Get-Job | Where-Object {$_.State -eq "Running"}).Count -ge $MaxThreads){
        Write-Verbose "Waiting for open thread...($MaxThreads Maximum)"
        Start-Sleep 3
    }
}

foreach($job in Get-Job){
    Receive-Job -Job $job -OutVariable temp
    Write-Host($temp)
}

Job Output example:

StartTime  : 9/7/2018 2:33:31 PM
OutFile    : C:\Scripts\PowerShell\Get-NMAPData\Test\localhost.xml
Arguments  : "-sS -sU -Pn -v"
Duration   : 00:00:01.0990367
Hash       : 
Target     : localhost
FinishTime : 9/7/2018 2:33:33 PM
RunspaceId : c68a80f5-198a-4af5-ac0d-c28667b3305c

@{StartTime=9/7/2018 2:33:31 PM; OutFile=C:\Scripts\PowerShell\Get-NMAPData\Test\localhost.xml; Arguments="-sS -sU -Pn -v"; Duration=00:00:01.0990367; Hash=; Target=localhost; FinishTime=9/7/2018 2:33:33 PM; PSComputerName=localhost; RunspaceId=c68a80f5-198a-4af5-ac0d-c28667b3305c; PSShowComputerName=False}
4
  • To have an array change to -ArgumentList ($server, '-sS -sU -Pn -v', 'C:\Scripts\PowerShell\Get-NMAPData\Test', 'C:\Program Files (X86)\nmap\nmap.exe') Commented Sep 7, 2018 at 19:15
  • No change. Jobs appear to run but I still am not getting the XML files that should be generated. Commented Sep 7, 2018 at 19:25
  • try changing your double quotes to single quotes for the variables: jon.netdork.net/2015/09/08/… Commented Sep 7, 2018 at 19:44
  • Cool, that did it. I can't believe I overlooked that. Commented Sep 7, 2018 at 20:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.