xml - Powershell Script - Merge Multiple Nessus Scans - -
so attempting automate process of merging multiple nessus scans, following manual guide defined @ ryker exum. challenge i'm having part have find , delete lines within files , including point (once specific string has been found). goal efficiently possible given of these nessus scan results (xml files) can on 100mb. approach to:
- put logic in place identify first , last file, , act accordingly on them.
- remove last 33 characters of first scan file come across.
- get content of each file , read each object in 1 @ time. if there not match, delete line , move on next object. if there match, delete line , stop (thus until).
at point, i've not had success getting step 3 work. code follows:
$first = get-childitem ".\" -filter *.nessus | select-object -first 1 $last = get-childitem ".\" -filter *.nessus | select-object -last 1 if ($first -ne $last) { get-childitem ".\" -filter *.nessus | foreach-object { $filepath = $_.fullname if ($first -eq $_ -and $last -ne $_) { $stream = [system.io.file]::openwrite($_.fullname) $stream.setlength($stream.length - 33) $stream.close() $stream.dispose() } if ($first -ne $_ -and $last -ne $_) { $stream = [system.io.file]::openwrite($_.fullname) $stream.setlength($stream.length - 33) $stream.close() $stream.dispose() $found = "" { get-content $_.fullname | foreach-object { $found = $_.contains("<report name=") if ($found) { where-object {$_ -match '<report name='} | set-content $filepath } else { where-object {$_ -notmatch '<report name='} | set-content $filepath } } } until ($found) } if ($last -eq $_ -and $first -ne $_) { $found = "" { get-content $_.fullname | foreach-object { $found = $_.contains("<report name=") if ($found) { where-object {$_ -match '<report name='} | set-content $filepath } else { where-object {$_ -notmatch '<report name='} | set-content $filepath } } } until ($found) } } }
thoughts or comments anybody?
i think looks complicated. instead check if it's first file, if i'd tell not skip lines, if isn't first file i'd have find <report name=
string, , skip lines , including that.
then i'd check if it's last file, , if have read whole rest of file, if it's not last file i'd have read last 2 lines of file beyond potentially reading.
once knew how many lines skip @ beginning and/or end of file i'd have read appropriate lines, , output new file, appending needed. code of looks this:
$first = gci ".\" -filter *.nessus|select -first 1 $last = gci ".\" -filter *.nessus|select -last 1 gci gci ".\" -filter *.nessus|%{ if($_.name -ne $first.name){$skiplines = (select-string -path $_.fullname -simplematch "<report name="|select -expand linenumber)+1}else{$skiplines = 0} if($_.name -ne $last.name){$readlines = (gc $_.fullname).count - 2 - $skiplines} else {$readlines = (gc $_.fullname).count - $skiplines} gc $_.fullname | select -first $readlines -skip $skiplines | out-file ".\merged.nessus" -append }
Comments
Post a Comment