最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

powershell - How to add a different named variable to a PSCustomObject in each loop of a for loop - Stack Overflow

programmeradmin1浏览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]
                     }
  }
}

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
  • 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/questions/70484662/… – Santiago Squarzon 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. – user66001 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? – Santiago Squarzon 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? – user66001 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. – user66001 Commented Mar 5 at 22:21
 |  Show 6 more comments

2 Answers 2

Reset to default 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

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论