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]
}
}
}
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]
}
}
}
Share
Improve this question
edited Mar 5 at 22:12
user66001
asked Mar 5 at 21:59
user66001user66001
9151 gold badge15 silver badges37 bronze badges
11
|
Show 6 more comments
2 Answers
Reset to default 1If 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
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
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/questions/70484662/… – Santiago Squarzon Commented Mar 5 at 22:09$ExternalUserDetails | Format-List
you should see that you actually had the objects there. Does that make sense? – Santiago Squarzon Commented Mar 5 at 22:20$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? – user66001 Commented Mar 5 at 22:20format-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. – user66001 Commented Mar 5 at 22:21