0

I wish to add a set of differently named variables as properties to a PSCustomObject. I tried the below code, but when seeing the properties of $ExternalUserDetails, I just get the final match. I have verified that $Matches[1] is getting the value intended, so I have omitted the $ExternalUserDetailsNeeded array contents, but it is a array of headers to information stored on the next line.

foreach($ExternalUserDetail in $ExternalUserDetailsNeeded) { $Matches = $null $DetailStartPosition = $TextBoxText.indexof($ExternalUserDetail) if($DetailStartPosition -ne -1) { $ExternalUserDetailEscaped = [Regex]::Escape($ExternalUserDetail) $TextBoxText.substring($DetailStartPosition) -match "(?s)$ExternalUserDetailEscaped\r?\n([^\r\n]*)" | out-null $ExternalUserDetails = [pscustomobject]@{} add-member -inputobject $ExternalUserDetails -membertype NoteProperty -name $ExternalUserDetail -value $Matches[1] } 

If I change it to the below, $ExternalUserDetails is only outputting the first match.

$ExternalUserDetails = foreach($ExternalUserDetail in $ExternalUserDetailsNeeded) { $Matches = $null $DetailStartPosition = $TextBoxText.indexof($ExternalUserDetail) if($DetailStartPosition -ne -1) { $ExternalUserDetailEscaped = [Regex]::Escape($ExternalUserDetail) $TextBoxText.substring($DetailStartPosition) -match "(?s)$ExternalUserDetailEscaped\r?\n([^\r\n]*)" | out-null [pscustomobject]@{ $ExternalUserDetails = $Matches[1] } } } 
11
  • You want a single custom object with multiple properties or one custom object per ExternalUserDetailsNeeded ? If the former, then the custom object should be outside the loop. If the latter, then you're facing the issue explained here: stackoverflow.com/questions/70484662/… Commented Mar 5 at 22:09
  • @SantiagoSquarzon Answer seems SO obvious when you one is given it. Think I will take a break from scripting for the day. Happy to give you correct answer/upvote if you make this into an answer. Commented Mar 5 at 22:17
  • Well it's due to whats explained in the linked answer, the table formatter is unable to determine what the properties of all objects in the collection are, it will use the properties of the first object to generate the table. But if you did $ExternalUserDetails | Format-List you should see that you actually had the objects there. Does that make sense? Commented Mar 5 at 22:20
  • But would also ask for another analysis. After putting the Object declaration before the loop, despite the order the array if foreach'd, I seem to be getting the final element (and only the final element) appearing at the start of the list, when outputting $ExternalUserDetails at the end of the looping. Made sure the loop wasn't somehow evaluating the array in the same order, by outputting `$ExternalUserDetail in each loop. Why? Commented Mar 5 at 22:20
  • 1
    @SantiagoSquarzon Yep, format-list displayed them in the correct order, but don't understand why, no. It is the last property to be added to the single object, not the first as mentioned would produce this result due to the answer in that other post. Commented Mar 5 at 22:21

2 Answers 2

1

If I understand your problem:

# [ordered] to save the order of the added properties $ExternalUserDetails = [ordered]@{} foreach ($ExternalUserDetail in $ExternalUserDetailsNeeded) { # $Matches = $null $DetailStartPosition = $TextBoxText.indexof($ExternalUserDetail) if ($DetailStartPosition -ne -1) { $ExternalUserDetailEscaped = [Regex]::Escape($ExternalUserDetail) $TextBoxText.substring($DetailStartPosition) -match "(?s)$ExternalUserDetailEscaped\r?\n([^\r\n]*)" | Out-Null foreach ($value in $Matches) { $ExternalUserDetails[$ExternalUserDetail] = $value } } } # if you later need a PS Object $ExternalUserDetailsObject = [PSCustomObject]$ExternalUserDetails 
Sign up to request clarification or add additional context in comments.

Comments

0

Try code below. You can add multiple Add-Member to each row as long as the NotePropertyName property is unique.

$table = [System.Collections.Generic.List[PSCustomObject]]::new(); $Mytable = 1..4 foreach($row in $MyTable) { $newRow = [PSCustomObject]::new() $title = 'Hello World' $newRow | Add-Member -NotePropertyName Row -NotePropertyValue $row $newRow | Add-Member -NotePropertyName Title -NotePropertyValue $title $table.Add($newRow) | out-null } $table | Format-Table 

1 Comment

ArrayList has been deprecated for powershell development since Powershell 3 or so. Use Generic lists(bonus: its .Add() returns [void]). And New-Object is inefficient for empty new object, use $newRow = [PSCustomObject]::new() because we don't know how many loops there will be.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.