So I am attempting to automate the process of merging multiple Nessus scans, following the manual guide defined at Ryker Exum. The challenge I'm having is the part where I have to find and delete lines within files up to and including a certain point (once a specific string has been found). My goal is to do this as efficiently as possible given some of these Nessus scan results (XML files) can be over 100MB. Thus my approach was to:
- Put some logic in place to identify the first and last file, and act accordingly on them.
- Remove the last 33 characters of all but the first scan file I come across.
- Get the content of each file and read each object in one at a time. if there is not a match, delete the line and move on to the next object. If there is a match, delete the line and stop (thus the do until).
At this point, I've not had any success getting step three to work. The code is as 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 = ""
do
{
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 = ""
do
{
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?