0

I'm dealing with a .csv export from Nessus, where essentially I have a column of Host IPs and a column with Plugin IDs

My customer wants an output where, for example, Plugin X would be in a column, and then next to it would be a comma separated list of affected Host IPs, and then next to THAT would be a count of the affected Host IPs.

Pic of what I'm looking for

After importing the Nessus CSV with Powershell, I was able to start to get what I needed with this:

$allfiltered | select-object 'Host IP','Plugin ID' | Where-Object 'Plugin ID' -like "57041" | Format-Table -Property  'Plugin ID','Host IP'

This gives me an output like this:

57041     10.1.1.1
57041     10.1.1.2
57041     10.1.1.3
57041     10.1.1.4
57041     10.1.1.5
57041     10.1.1.6
57041     10.1.1.7

But as you can see, I have a long way to go to pull this into the output I need (see pic above).

I think I'm eventually going to need a for loop to get all the plugin values assessed, but I need to figure out how to essentially query for "Take all IPs that match plugin X and place them into a comma separated list" and go from there.

Can you help steer me in the right direction?

-B

4

1 回答 1

0

Group-Object只需使用CmdLet即可完成您想要的操作。

例如,像这样的东西会将所有插件组合在一起,然后您可以迭代这些组,将 ips 以逗号分隔的字符串连接在一起,然后从那里做任何您想做的事情。

$Grouped = $List | group -Property PluginID | sort PluginID

Foreach ($Group in $Grouped) {
   $IpsDelimited = ($group.Group | Select -ExpandProperty IP | Sort) -join ','
   Write-Host "Plugin ID: $($Group.Name) " -ForegroundColor Cyan -NoNewline
    Write-Host $IpsDelimited

}

这是我用于此示例的示例列表。

# The list is for example purpose based on what you have. 
$List = @(
    new-object psobject -Property @{'PluginID'='57041 ';IP='10.1.1.1'}
    new-object psobject -Property @{'PluginID'='57041 ';IP='10.1.1.3'}
    new-object psobject -Property @{'PluginID'='57041 ';IP='10.1.1.4'}
    new-object psobject -Property @{'PluginID'='57042 ';IP='10.1.1.6'}
    new-object psobject -Property @{'PluginID'='57042 ';IP='10.1.1.7'}
    new-object psobject -Property @{'PluginID'='57043 ';IP='10.1.1.8'}
    new-object psobject -Property @{'PluginID'='57043 ';IP='10.1.1.9'}
    new-object psobject -Property @{'PluginID'='57044 ';IP='10.1.1.10'}
)

结果输出

结果输出

为了娱乐

如果要将其重新导出为 CSV 文件,则可以使用计算属性直接操作输出对象并使用修改后的信息将其导出。

$Output = $Grouped |
Select  @{'N'='PluginID';E={$_.Name}},
        @{N='IPSDelimited';E={($_.Group | Select -ExpandProperty IP | Sort) -join ','}},
        @{N='My other field';E={$_.Group.OtherField}}

$Output | Export-Csv -Path 'MyPath' -NoTypeInformation
于 2017-10-06T00:34:55.163 回答