• 0

PowerShell Break Loop Problem


Question

I'm creating a script that will get a list of Cluster objects from Active Directory, iterate through them comparing the cluster node names to a name that is input earlier in the script and add the cluster that contains the matching node name to a PSObject. I'm accomplishing this with a nested ForEach loop and the break works great on the internal loop, but the break on the containing ForEach loop just stops the script entirely instead of breaking out of that loop and I'm not sure why. Would someone mind taking a look at my code block and seeing if they can see why that's happening? Thanks!

$clusters = Get-Cluster -Domain my.domain.org

$clusterNodeFound = $false
ForEach ($cluster in $clusters) {
    if (-Not $clusterNodeFound) {
        $clusterNodes = Get-ClusterNode -Cluster $cluster | Select-Object Name, State
    }
    else {
        break
    }

    ForEach ($clusterNode in $clusterNodes) {
        if ($clusterNode.Name -eq $sqlServer) {
            $sqlServerObject | Add-Member -MemberType NoteProperty -Name clusterName -Value $cluster -Force
            $clusterNodeFound = $true
            break
        }
    }
}

 

Link to comment
https://www.neowin.net/forum/topic/1329964-powershell-break-loop-problem/
Share on other sites

1 answer to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.