Skip to content

Instantly share code, notes, and snippets.

@codegard1
Last active March 3, 2020 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codegard1/c6f7bf8516a4b702331240641593bdf3 to your computer and use it in GitHub Desktop.
Save codegard1/c6f7bf8516a4b702331240641593bdf3 to your computer and use it in GitHub Desktop.
Flatten List Test
Function Get-ParentRows {
<#
.SYNOPSIS
Returns rows that have no Parent ID and where Comments is not blank
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Array]$CSVData
)
# Get the parent rows by filtering
$ParentRows = $CSVData | Where-Object { $_.Parent -eq '' -and $_.Comments -ne '' }
$Hash = New-Object PSCustomObject
ForEach( $ParentID in $ParentRows.ID ){
$Hash | Add-Member Noteproperty $ParentID @($ParentID)
}
Return $hash
}
# Remove this line for production
$SourceFile = "I:\MacroProj\Chris\Reilly Windows Test 2.csv"
$CSVData = Import-CSV $SourceFile -Encoding UTF8
$Hash = Get-ParentRows $CSVData
$Childrows = $CSVData | Where-Object { $_.Parent -ne '' } |sort
$Counter = $Childrows.Count
While ($Counter -gt 0){
ForEach( $Row in $Childrows ){
$ID = $Row.ID
$ParentID = $Row.Parent
Write-Host $Counter $ID $ParentId -ForegroundColor Cyan
ForEach( $List in $Hash.PSObject.Properties.Name ){
#Write-Host "`t$($Hash.$list)" -ForegroundColor Magenta
If(($Hash.$List -contains $ParentID) -and ($Hash.$List -notcontains $ID)){
#Write-Host "`t$ParentID" -ForegroundColor Yellow
$Hash.$List += $ID
$Counter--
}
}
#$Hash
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment