davehull / Kansa

A Powershell incident response framework
Apache License 2.0
1.56k stars 266 forks source link

Rolling Sweep #139

Closed mbaker2111 closed 7 years ago

mbaker2111 commented 9 years ago

I have used Kansa to sweep a large set of systems. I am running into resource problems when running against more the 5k remote systems. Any way to add a feature that would allow you to specify the target count and then once its completed move onto the next set within the target list?

athegist commented 9 years ago

For host sets larger than 3 to 4 thousand hosts, you might sAplit the target file up into multiple parts and run separate instances of PowerShell.exe & Kansa.ps1 against each host set. You can do this from a single host if the numbers don't get too large. For larger host sets, multiple hosts may be in order.

A simple script like this that calls Kansa and breaks a large hosts.txt file up into $BatchSize pieces may be helpful:

$ErrorActionPreference = "Continue"

# Number of hosts to process with Kansa.ps1 at one time
$BatchSize = 200   

# Let's make a copy of the host list
Copy-Item .\hosts.txt .\remaining.txt

While (( Get-Content .\remaining.txt).Length )
{
    $inprogress = Get-Content .\remaining.txt -First $BatchSize
    $inprogress | Set-Content -Encoding Ascii -Force .\inprogress.txt
    $remaining  = Get-Content .\remaining.txt

    if (-not(Compare-Object $inprogress $remaining)) 
    {
        # $inprogress and $remaining arrays match, so we've already 
        # processed $remaining. Clean up after ourselves.
        "Done."
        Remove-Item .\remaining.txt
        Remove-Item .\inprogress.txt
        exit
    }

    # Copy the remaining hosts to .\remaining.txt
    $remaining | Where-Object { $_ -notin $inprogress } | Set-Content -Force -Encoding Ascii .\remaining.txt
    "Replace this line with call to .\Kansa.ps1 -TargetList .\inprogress and other args..."
}
athegist commented 9 years ago

Of course if you do something like this, it's on you to combine the output from all of the Output... directories for analysis. My advice, copy the data when you combine, don't move it, then if you screw up, you can try again. Good luck.

jt-msft commented 8 years ago

We also have some enhancements in the pipeline to help with resource exhaustion and improve parallelization of work.