Update a Value in an Array When Using Match Operator

This topic contains 1 reply, has 1 voice, and was last updated by  AK 6 months, 1 week ago.

  • Author
    Posts
  • #68764

    AK
    Participant

    Hello Everyone,

    I have this array:

    $hashtable = @{'NAME' = "Name1";
    'NODE' = "Node1";
    
     $node = New-Object -TypeName psobject -Property $hashtable
     $node.psobject.TypeNames.Insert(0,"MyTest")
     $nodess +=  $node
     
    # add several nodes (Node2, Node3, etc.) to this array then use -match to find a node
    
    if ($node.Name -match $mynodename)
                {
                $node.NODE = "NewNode"
                }
    
    

    I'd like to use -match to find and update a record per example above. Is there a good way to do this without doing a "for each" and finding an index?
    I get this error when trying to update a value:
    The property 'NODE' cannot be found on this object. Verify that the property exists and can be set.

  • #68803

    AK
    Participant

    Alright, ended up using

     $index = $node.Name.Indexof($mynodename)
     $node[$index].NODE = "NewNode"

You must be logged in to reply to this topic.