簡體   English   中英

Powershell Function 返回哈希表

[英]Powershell Function return HashTable

我有這個 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 等。

如果我像這樣從腳本中調用 function

BuildDaHash($string)

我將獲得 hash 表的 output,但如果我想獲得 output 到一個變量,所以我可以使用它,它是空的。 意思是如果我這樣做:

$test = BuildDaHash($string)

它是空的,我在這里錯過了什么? 我想也許是在像這樣之前建造一個空的 hash :

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

但這也是空的

*編輯我知道 Nmap 有不同的可用格式,我使用默認 txt,我有另一個 function 讀取 output 並僅返回一個帶有 Z957B527BFBAD2E80F58D2068/filtered3931 的 txt 的示例是 $3Z 字符串的示例:

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

正如所評論的,您不應該在參數周圍使用括號。 相反,在 PowerShell 中,您使用空格分隔參數。

這里的主要問題是你認為你發送的數據是一個字符串,但實際上它應該是一個字符串數組,因為 function 期望這樣。

盡管我不知道您要使用生成的嵌套哈希表實現什么目標,但請嘗試:

$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

使用開關和自定義對象來保存結果怎么樣?

switch -Regex ($_) {
    'Nmap' {            
        if ($current -ne $null) {$current}
        $current = [PSCustomObject]@{
            ip = ($_ -split "for ")[1]
            ports = @()
        }
    }
    '^\d+' {
        $port,$transport,$filtered,$protocol = ($_  -split '/' -split '\s+')            
        $current.ports += [PSCustomObject]@{
            port = $port
            transport = $transport
            filtered = $filtered
            protocol = $protocol
        }            
    }        
}    

測試

@'
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" | % {
    switch -Regex ($_) {
        'Nmap' {            
            if ($current -ne $null) {$current}
            $current = [PSCustomObject]@{
                ip = ($_ -split "for ")[1]
                ports = @()
            }
        }
        '^\d+' {
            $port,$transport,$filtered,$protocol = ($_  -split '/' -split '\s+')            
            $current.ports += [PSCustomObject]@{
                port = $port
                transport = $transport
                filtered = $filtered
                protocol = $protocol
            }            
        }        
    }    
}

我發現了我的錯誤。 Function BuildDaHash 獲取一個參數,一旦它使用文件的 get-content 獲取它的參數,它就可以工作,但是一旦它從一個字符串中獲取(即使使用 get-content 會給出的相同的 txt)它也不會工作,原因是 get- content 是一個字符串數組,每行為 object,字符串只是一個大文本數據。 對不起noobness 謝謝大家的幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM