Last active
March 3, 2020 04:40
-
-
Save codegard1/c6f7bf8516a4b702331240641593bdf3 to your computer and use it in GitHub Desktop.
Flatten List Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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