I have the following function:
Function Refine{
[CmdletBinding()]
param (
[parameter(ValueFromPipeline = $true)]
$data
)
begin {}
process {
For ($i=0; $i -le $data.count; $i++){
if ($data[$i] -match 'nmap scan report' -and $data[$i+1] -match 'host is up' ) {
$dat += $data[$i] + "`n"
$counter=($i+4)
while ($data[$counter] -match '^[0-9]') {
$dat += $data[$counter] + "`n"
$counter++
}
}
}
}
end {
if ($dat){#See that its not empty
return $dat.Split([Environment]::NewLine)
}
}
}
$file1 = gc c:\test.txt
,gc ($file1) | Refine
I must pipe with the Unary operator since i want the whole object to be piped at one time to the Refine Function.
The output is something like this for example: Nmap scan report for 1.1.1.1 113/tcp closed ident 443/tcp open ssl/http Fortinet SSL VPN Nmap scan report for 2.2.2.2 21/tcp filtered ftp 22/tcp filtered ssh
I now would like to use the another function which takes this and create objects from text:
Function BuildArray{
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline = $true)]
$data
)
Begin {}
Process {
$NMAPS=@()
$nmap=@()
$ports=@()
For ($i=0; $i -lt $data.count; $i++){
If ($data[$i] -match 'nmap scan report') {
$ip = ($data[$i] | Select-String -Pattern "\d{1,3}(\.\d{1,3}){3}" -AllMatches).Matches.Value
$k=$i
If ($data[($i+1)] -notmatch 'nmap scan report' -and $i -le $data.count){
DO{
$ports+=$data[$i+1]
$i++
}
While ($data[$i+1] -notmatch 'nmap scan report' -and $i -le $data.count)
$nmap = @{IP = "$ip"; ports=@($ports)}
$NMAPS+=$nmap
$nmap=@()
$ports=@()
$i = $k
}
}
}
}
End {
$NMAPS
}
}
My problem here is again - i would like the data from the pipe to pass as a whole to the BuildArray function. but i cannot use the unary operator since i get an error saying i can use it only at the start of the piping,
,(gc $file1 ) | ,(refine) | BuildArray #No good
How Can i pass this second object in the pipline as a whole to the third object once at once?