• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

PowerShell Break Loop Problem

Question

Stokkolm    328

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
        }
    }
}

 

Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0
Stokkolm    328

My apologies, I figured it out. I had to place the child ForEach inside the If block and it now works properly. Doh!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.