We have the following text file:
[UserA]
;Path1 in comment
Path 2
Path 3
[UserB]
Path 1
[UserC]
Path 1
Path 2
We're trying to create one object per user with the properties SamAccountName and Path. The following code does this but it's not able to catch the last object:
Param (
[String]$File = 'S:\Test\Brecht\Input_Test\files.ini'
)
#<# TEST
$VerbosePreference = 'Continue'
#>
$Hash = @{}
$Path = @()
$FileContent = Get-Content $File | where {$_ -notlike ';*'}
$Objects = $FileContent | ForEach-Object {
Write-Verbose "Text line '$_'"
if ($_ -match '\[') {
if ($Path.Length -ne 0) {
$Hash.Path = $Path
New-Object –TypeName PSObject -Property $Hash
}
$Hash = @{}
$Path = @()
$Hash.SamAccountName = $_.Split('[,]')[1]
}
else {
Write-Verbose "Add path '$_'"
$Path += [PSCustomObject]@{
Original = $_
Test = $null
}
}
}
Is there a better way to do this?