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] } } }
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/…$ExternalUserDetails | Format-Listyou should see that you actually had the objects there. Does that make sense?$ExternalUserDetailsat 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?format-listdisplayed 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.