Update Split-ObjectList.ps1

This commit is contained in:
freedbygrace
2023-10-22 20:42:27 -04:00
committed by GitHub
parent f286d6276d
commit 62b928ffa8
+33 -3
View File
@@ -8,7 +8,7 @@ Function Split-ObjectList
Provides the functionality to split a list of objects into chunks based on the specified mode. Provides the functionality to split a list of objects into chunks based on the specified mode.
.DESCRIPTION .DESCRIPTION
This is the more performant method of breaking an object into chunks for various use cases. This is the more performant method of breaking an object into chunks for various use cases. A list of hundreds of thousands of objects can be broken apart in seconds.
.PARAMETER ByPercentage .PARAMETER ByPercentage
Places the function into "ByPercentage" mode. This will split the list of objects based on the provided percentage list. Places the function into "ByPercentage" mode. This will split the list of objects based on the provided percentage list.
@@ -46,7 +46,7 @@ Function Split-ObjectList
Write-Output -InputObject ($SplitObjectListResult) Write-Output -InputObject ($SplitObjectListResult)
.NOTES .NOTES
Most of the time, various "List" type objects are used with index removal and so forth, which can work, but is not the most performant. If the list of input objects cannot be divided evenly, the stragglers will be automatically added to the final wave.
.LINK .LINK
https://stackoverflow.com/questions/56990719/split-array-by-percentage https://stackoverflow.com/questions/56990719/split-array-by-percentage
@@ -223,6 +223,8 @@ Function Split-ObjectList
$PercentageListCounter = 1 $PercentageListCounter = 1
$PercentageListCount = ($PercentageList | Measure-Object).Count $PercentageListCount = ($PercentageList | Measure-Object).Count
$PercentageListUpperBound = $PercentageList.GetUpperBound(0)
For ($PercentageListIndex = 0; $PercentageListIndex -lt $PercentageListCount; $PercentageListIndex++) For ($PercentageListIndex = 0; $PercentageListIndex -lt $PercentageListCount; $PercentageListIndex++)
{ {
@@ -238,7 +240,7 @@ Function Split-ObjectList
{ {
{($_ -eq $True)} {($_ -eq $True)}
{ {
$InputObjectPercentageFormula = {[System.Math]::Round((($InputObjectListCount * $Percentage) / 100), 0)} $InputObjectPercentageFormula = {[System.Math]::Floor((($InputObjectListCount * $Percentage) / 100))}
$InputObjectPercentageCalculation = $InputObjectPercentageFormula.InvokeReturnAsIs() $InputObjectPercentageCalculation = $InputObjectPercentageFormula.InvokeReturnAsIs()
@@ -266,6 +268,34 @@ Function Split-ObjectList
$OutputObjectProperties.Members.Add($InputObject) $OutputObjectProperties.Members.Add($InputObject)
} }
Switch ($PercentageListIndex)
{
{($_ -eq $PercentageListUpperBound)}
{
Switch ($InputObjectQueue.Count -gt 0)
{
{($_ -eq $True)}
{
$LoggingDetails.LogMessage = "$($GetCurrentDateTimeMessageFormat.Invoke()) - The $($InputObjectListCount) input object(s) could not be divided evenly."
Write-Verbose -Message ($LoggingDetails.LogMessage)
$LoggingDetails.LogMessage = "$($GetCurrentDateTimeMessageFormat.Invoke()) - Attempting to add the $($InputObjectQueue.Count) remaining queue member(s) to wave $($OutputObjectProperties.Wave). Please Wait..."
Write-Verbose -Message ($LoggingDetails.LogMessage)
While ($InputObjectQueue.Count -gt 0)
{
$InputObject = $InputObjectQueue.Dequeue()
$OutputObjectProperties.Members.Add($InputObject)
}
$LoggingDetails.LogMessage = "$($GetCurrentDateTimeMessageFormat.Invoke()) - New Wave Member Count: $($OutputObjectProperties.Members.Count)"
Write-Verbose -Message ($LoggingDetails.LogMessage)
}
}
}
}
$OutputObjectProperties.Members = $OutputObjectProperties.Members.ToArray() $OutputObjectProperties.Members = $OutputObjectProperties.Members.ToArray()
$OutputObjectProperties.MemberCount = $OutputObjectProperties.Members.Count $OutputObjectProperties.MemberCount = $OutputObjectProperties.Members.Count