Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Function return HashTable

I have this function:

Function BuildDaHash($data){
$arr=@{}
    $j=0
    $k=0
    For ($i=0; $i -lt $data.count; $i++){
        if ($data[$i] -match 'nmap scan report')  {
            $arr[$j]=@{}
            $arr[$j][$k] = $data[$i]
            $i++
            $k++
            Do {
               $arr[$j][$k] = $data[$i].Split(' ')[0]
               $i++
               $k++
               } While ($data[$i] -notmatch 'nmap scan report' -and $i -lt $data.count) 
           $j++
           $k=0
           $i--
        }
      }
      return $arr
}

this function takes a long string (Nmap scan report) and build Hash table so i can then address it as follows: output of $arr[0][0] is 192.168.20.10 and then followed by ports like: $arr[0][1] = 22; $arr[0][2] = 80, etc.

If i call the function from the script like so

BuildDaHash($string)

i will get the output of the hash table, but if i want to get the output to a varbiable so i can work with it, it comes empty. meaning if i do this:

$test = BuildDaHash($string)

it comes empty, what am i missing here? i thought maybe it something with building an empty hash before like so:

$tst =@{}
$tst = BuildDaHash($string)

But that also comes empty

*EDIT I know Nmap has different formats available, im working with default txt, i have another function that reads the output and return a txt only with the ip and the open/filtered ports here is an example of $string:

Nmap scan report for 11.11.111.111
21/tcp  filtered ftp
179/tcp filtered bgp
646/tcp filtered ldp
Nmap scan report for 22.22.222.12
21/tcp    filtered ftp
111/tcp   filtered rpcbind
179/tcp   filtered bgp
646/tcp   filtered ldp
like image 818
Shahar Avatar asked Jan 21 '26 20:01

Shahar


1 Answers

As commented, you should not use brackets around parameters. Instead, in PowerShell you separate parameters by spaces.

The main issue here is that you think the data you're sending is a string, but in fact it should be a string array, because the function expects that.

Although I have no idea what you're trying to achieve with the resulting nested hashtable , but try:

$string = @"
Nmap scan report for 11.11.111.111
21/tcp  filtered ftp
179/tcp filtered bgp
646/tcp filtered ldp
Nmap scan report for 22.22.222.12
21/tcp    filtered ftp
111/tcp   filtered rpcbind
179/tcp   filtered bgp
646/tcp   filtered ldp
"@ -split '\r?\n'   # break the string down to a string array

$test = BuildDaHash $string

$test
like image 57
Theo Avatar answered Jan 23 '26 14:01

Theo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!