【发布时间】:2020-01-02 23:05:33
【问题描述】:
我尝试将[PSCustomObect] 中的 8 个对象导出到 csv。它只是有点工作。
数据在查看器中组织得很好。
但实际的 CSV 导出是一团糟。每个 RegEx 匹配 (PSCustomerObject) 都是一个新行,而不是每个设备一行(而是每个 foreach 循环对象一行)
我在这里缺少什么?
$content = Get-Content -Path "U:\Dump\dump.nmap"-RAW
#$RegX_splitter = '(?m)(?=^Nmap scan report for )'
$RegX_DeviceIP = '(?sm)Nmap scan report for\s*(?<DeviceIP>.*?)$'
$RegX_DeviceType = '(?sm)Device type:\s*(?<DeviceType>.*?)$'
$RegX_OS = '(?sm)Running:\s*(?<OS>.*?)$'
$RegX_OSdetails = '(?sm)OS details:\s*(?<OSdetails>.*?)$'
$RegX_Servicesplit='(?sm)(?=^23\/tcp)(?<port>\d+\/\w+)\s+(?<state>\w*?)\s+(?<service>\w*?)\s+(?<description>.*?)$'
$counter = 0
$data = $content -split 'TRACEROUTE' | ForEach-Object{
$counter = $counter + 1
write-host "============================================="
write-host $counter
write-host "============================================="
if ($_ -match $RegX_DeviceIP){
write-host 'The Device Name (IP) is:'$Matches.DeviceIP
}
if ($_ -match $RegX_Servicesplit){
write-host 'The port number is:'$Matches.Port
write-host 'The port is:'$Matches.State
write-host 'The service running is:'$Matches.Service
write-host 'The Service description is:'$Matches.Description
}
if ($_ -match $RegX_DeviceType){
write-host 'The Device Type is:'$Matches.DeviceType
}
if ($_ -match $RegX_OS){
write-host 'The OS is:'$Matches.OS
}
if ($_ -match $RegX_OSdetails){
write-host 'The OS details are:'$Matches.OSdetails
}
[PSCustomObject]@{
DeviceIP = $Matches.DeviceIP
Port = $Matches.Port
State = $Matches.State
Service = $Matches.Service
Description = $Matches.Description
DeviceType= $Matches.DeviceType
OS = $Matches.OS
OSdetails = $Matches.OSdetails
}
}
$data | Export-Csv "U:\Dump\testexport.csv" -NoTypeInformation
$data | Format-Table -AutoSize
示例输入
Nmap scan report for 10.0.0.1
Host is up (0.000060s latency).
Not shown: 997 closed ports
PORT STATE SERVICE VERSION
23/tcp open telnet SMC SMC2870W Wireless Ethernet Bridge
80/tcp open tcpwrapped
| http-auth:
| HTTP/1.1 401 Unauthorized\x0D
|_ Basic realm=GatewayAdmin
|_http-title: Site doesn't have a title (text/html).
5060/tcp open sip (SIP end point; Status: 200 OK)
| fingerprint-strings:
| SIPOptions:
| SIP/2.0 200 OK
| Content-Type:application/sdp
| Supported:replaces,100rel,timer
| Allow:INVITE,ACK,OPTIONS,BYE,CANCEL,REGISTER,INFO,PRACK,REFER,NOTIFY
| From:<sip:nm@nm>;tag=root
| To:<sip:nm2@nm2>;tag=4EFF32463135364101C48D67
| Call-ID:50000
| CSeq:42 OPTIONS
| Server:PBX-IP Media Gateway/2.1
| Via:SIP/2.0/TCP nm;branch=foo;received=10.0.232.59
|_ Content-Length:0
1 service unrecognized despite returning data. If you know the service/version, please submit the following fingerprint at https://nmap.org/cgi-bin/submit.cgi?new-service :
SF-Port5060-TCP:V=7.80%I=7%D=8/28%Time=5D669285%P=i686-pc-windows-windows%
SF:r(SIPOptions,168,"SIP/2\.0\x20200\x20OK\r\nContent-Type:application/sdp
SF:\r\nSupported:replaces,100rel,timer\r\nAllow:INVITE,ACK,OPTIONS,BYE,CAN
SF:CEL,REGISTER,INFO,PRACK,REFER,NOTIFY\r\nFrom:<sip:nm@nm>;tag=root\r\nTo
SF::<sip:nm2@nm2>;tag=4EFF32463135364101C48D67\r\nCall-ID:50000\r\nCSeq:42
SF:\x20OPTIONS\r\nServer:PBX-IP\x20Media\x20Gateway/2\.1\r\nVia:SIP/2\.0/T
SF:CP\x20nm;branch=foo;received=10\.0\.232\.59\r\nContent-Length:0\r\n\r\n
SF:");
Device type: VoIP phone
Running: Aastra embedded
OS details: Aastra Dialog 4425 IP phone
Network Distance: 4 hops
Service Info: Device: bridge
TRACEROUTE (using port 22/tcp)
HOP RTT ADDRESS
1 0.00 ms 10.X.X.254
2 0.00 ms 10.X.X.73
3 0.00 ms 10.X.X.1
4 0.00 ms 10.X.X.13
【问题讨论】:
-
$data导出前的类型是什么?$data.getType()? -
不确定我是否理解。我编写了其他类似的脚本,我只是在创建 foreach 循环的同时调用 $data。
-
既然你混合了大量的
Write-Host,我猜它要么是一个大字符串数组,要么是一个here-string。您的输出不是 PSCustomObject,因此它不会通过管道传输到 export-csv 中,而 PSCustomObject 属性中的每个项目都是单列中的值。我的工作机器上没有nmap,但会在家里看看,这里也无法查看i.stack.imgur -
它将每个 PSCustomObject 作为一个值添加到 csv 中,只是每个都是 CSV 中的新行。我希望 ForEach 循环中的每个匹配项都在一行中。所有的数据都在正确的列下,只是行到处都是。
-
谢谢你。几个小时后我会看看这个
标签: powershell