0

I'm suspect I am going about this the wrong way and need to switch to a hash table but is it possible to update a specific value in an Array? The script retrieves the cluster nodes and stores the data in a array, then proceeds to reboot each one, once rebooted I want to update the reboot 'column' value in the array to 1.

$Array=@()
$Cluster = Get-ClusterNode
foreach ($Node in $Cluster)
    {                 
        $item = New-Object PSObject
        $item | Add-Member -type NoteProperty -Name 'Node' -Value $Node.Name
        $item | Add-Member -type NoteProperty -Name 'State' -Value $Node.State
        $item | Add-Member -type NoteProperty -Name 'Rebooted' -Value "0"
        $Array += $item
    }
foreach ($row in $Array | Where { $Array.Rebooted -eq "0" })
    {
    Reboot-Function -Node $row.Node
    $Array.Rebooted += 1 | Where-Object {$Array.Node -eq $row.Node}
    }
$Array
2
  • 1
    You already have a reference to the current item in the array, in the form of $row. Simply do $row.Rebooted = 1 instead of $Array.Rebooted += 1 | Where-Object {$Array.Node -eq $row.Node} Commented Mar 24, 2022 at 16:20
  • In addition, Where { $Array.Rebooted -eq "0" } doesn't seem to be needed at all, since -Name 'Rebooted' -Value "0". Commented Mar 24, 2022 at 16:24

1 Answer 1

1

You need to rewrite the Where-Object statement in the second loop slightly (use $_ to refer to the current pipeline object), and then simply update the current object via the $row variable inside the loop body:

foreach ($row in $Array | Where { $_.Rebooted -eq "0" })
{
    Reboot-Function -Node $row.Node
    $row.Rebooted = '1'
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.