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}
-ArgumentList ($server, '-sS -sU -Pn -v', 'C:\Scripts\PowerShell\Get-NMAPData\Test', 'C:\Program Files (X86)\nmap\nmap.exe')