Skip to content

Commit

Permalink
Update psf-devices.ps1
Browse files Browse the repository at this point in the history
Issue #369: Removed erroneous re-use of `$HostList` variable during filtered searches.

Increased performance when using pipeline to search for hostnames by adding pipeline values as a range (i.e. once) instead of individually (in `foreach` loop).
  • Loading branch information
bk-cs committed Nov 22, 2023
1 parent 83fe32b commit aeba5e1
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions public/psf-devices.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -133,33 +133,37 @@ https://github.com/crowdstrike/psfalcon/wiki/Find-FalconHostname
[string[]]$Array
)
begin {
if ($Path) {
[string]$Path = $Script:Falcon.Api.Path($Path)
} else {
[System.Collections.Generic.List[string]]$List = @()
}
[System.Collections.Generic.List[string]]$List = @()
if ($Path) { [string]$Path = $Script:Falcon.Api.Path($Path) }
[string[]]$Select = 'device_id','hostname'
if ($Include) { $Select += $Include }
}
process { if ($Array) { @($Array).foreach{ $List.Add($_) }}}
process {
if ($Path) {
$List.AddRange([string[]]((Get-Content -Path $Path).Normalize()).Where({
![string]::IsNullOrWhiteSpace($_) }))
} elseif ($Array) {
$List.AddRange([string[]]@($Array).Where({ ![string]::IsNullOrWhiteSpace($_) }))
}
}
end {
[string[]]$HostList = if ($List) { $List } else { (Get-Content -Path $Path).Normalize() }
$HostList = $HostList | Select-Object -Unique | Where-Object { ![string]::IsNullOrEmpty($_) }
for ($i=0; $i -lt ($HostList | Measure-Object).Count; $i+=100) {
[string[]]$TempList = $HostList[$i..($i + 99)]
[string]$Filter = if ($Partial) {
(@($TempList).foreach{ "hostname:'$_'" }) -join ','
} else {
(@($TempList).foreach{ "hostname:['$_']" }) -join ','
}
[object[]]$HostList = Get-FalconHost -Filter $Filter -Detailed | Select-Object $Select
@($TempList).foreach{
if (($Partial -and $HostList.hostname -notlike "$_*") -or (!$Partial -and
$HostList.hostname -notcontains $_)) {
$PSCmdlet.WriteWarning("[Find-FalconHostname] No match found for '$_'.")
if ($List) {
$List = $List | Select-Object -Unique
for ($i=0; $i -lt ($List | Measure-Object).Count; $i+=100) {
[string[]]$Group = $List[$i..($i+99)]
[string]$Filter = if ($Partial) {
(@($Group).foreach{ "hostname:'$_'" }) -join ','
} else {
(@($Group).foreach{ "hostname:['$_']" }) -join ','
}
$Req = Get-FalconHost -Filter $Filter -Detailed | Select-Object $Select
@($Group).foreach{
if (($Partial -and $Req.hostname -notlike "$_*") -or (!$Partial -and $Req.hostname -notcontains $_)) {
$PSCmdlet.WriteWarning("[Find-FalconHostname] No match found for '$_'.")
}
}
if ($Req) { $Req }
}
if ($HostList) { $HostList }
}
}
}

0 comments on commit aeba5e1

Please sign in to comment.