locked
Foreach Loop can't break when variable is Yes RRS feed

  • Question

  • Hi All , 

    I created a function to look for a userID in two sets of AD groups , Group A consist of 5 security groups , Group B consists also of another different 5 security groups

    If a user is found at least in 1 of both sets of groups then variable $founduser will equal Yes .... the problem is , I want if user is found after the first search and the found user variable = yes then i want it to break and not go search for the other groups , but that never happens ... it keep going till it finish all groups .

    Please advise what is wrong with my code

    Function CheckSource
    {

    write-host "Found user status" $FoundUser
    Foreach ($g in $Group21 )
    {
    if((Get-content -Path "$GroupsPath\$g1b" | Where-Object {$_ -contains $username}) -And (Get-content -Path "$GroupsPath\$g" | Where-Object {$_ -contains $username}))


    {

    $FoundUser ="Yes"
    Write-Host $FoundUser
    Write-host "Data Source found"
    Write-Host "AppData: Physical data location: \\ServerA"

    break



    }


    }}

    Thursday, October 3, 2019 7:40 PM

Answers

  • This is how to use "contains"

    Function CheckSource{
    
    	Foreach ($g in $Group21 ){
    		$test1 = (Get-content -Path "$GroupsPath\$g1b") -contains $username
    		$test2 = (Get-content -Path "$GroupsPath\$g") -contains $username
    		if($test1 -and $test2){
    			Write-Host "Found User $test1"
    			Write-Host 'AppData: Physical data location: \\ServerA'
    			break
     		}
    	}
    }

    "contains" and "in" only work for collections.

    Always test your logic at a prompt until you know it is correct.

    In this code the string must exactly match the complete line.


    \_(ツ)_/



    • Edited by jrv Thursday, October 3, 2019 8:49 PM
    • Marked as answer by springhh Friday, October 4, 2019 1:30 AM
    Thursday, October 3, 2019 8:46 PM

All replies

  • Please format and post your code correctly using correct indenting. I am sure when you do this you will see your mistakes.

    Please read the following first:


    \_(ツ)_/

    Thursday, October 3, 2019 8:08 PM
  • Function CheckSource
        {
    
          write-host "Found user status" $FoundUser
          Foreach ($g in $Group21 )
               {
                 if((Get-content -Path "$GroupsPath\$g1b" | 
                 Where-Object {$_ -contains $username}) -And 
                 (Get-content -Path "$GroupsPath\$g" | Where- 
                 Object {$_ -contains $username}))
    
    
                     {
    
                        $FoundUser ="Yes"
                        Write-Host $FoundUser
                        Write-host "Data Source found"
                        Write-Host "AppData: Physical data 
                                    location: \\ServerA"
    
                        break
    
    
    
                     }
    
    
                 }
          }
    I tried to nest it but still can't figure out what is the issue .... could it because i'm not using Else ? 
    Thursday, October 3, 2019 8:42 PM
  • Obviously it is not finding anything.

    "contains" is not going to work.  You need to use "eq" or "match"


    \_(ツ)_/

    Thursday, October 3, 2019 8:44 PM
  • This is how to use "contains"

    Function CheckSource{
    
    	Foreach ($g in $Group21 ){
    		$test1 = (Get-content -Path "$GroupsPath\$g1b") -contains $username
    		$test2 = (Get-content -Path "$GroupsPath\$g") -contains $username
    		if($test1 -and $test2){
    			Write-Host "Found User $test1"
    			Write-Host 'AppData: Physical data location: \\ServerA'
    			break
     		}
    	}
    }

    "contains" and "in" only work for collections.

    Always test your logic at a prompt until you know it is correct.

    In this code the string must exactly match the complete line.


    \_(ツ)_/



    • Edited by jrv Thursday, October 3, 2019 8:49 PM
    • Marked as answer by springhh Friday, October 4, 2019 1:30 AM
    Thursday, October 3, 2019 8:46 PM
  • Thanks for your help but same problem happened .... it found the userID many times , i want it to stop if it found it even once . look at the output

    I don't know why "break" doesn't work

    Found User True

    AppData: Physical data location: \\ServerA
    Found User True
    AppData: Physical data location: \\ServerA
    Found User True
    AppData: Physical data location: \\ServerA

    Found User True

    Thursday, October 3, 2019 11:44 PM
  • Change "break" to "return" and it will work.

    \_(ツ)_/

    Thursday, October 3, 2019 11:51 PM
  • Still return doesn't work .... it works when i use Exit(0) but what if I don't want it to exit .

    I have a mistake somewhere

    Thursday, October 3, 2019 11:57 PM
  • You didn't use the code I posted did you?  You changed your code and found it doesn't work.


    \_(ツ)_/

    Friday, October 4, 2019 12:00 AM
  • I used your script exactly as it is , but I also have other functions in my script ... so I think the problem in the rest of my script and not in this piece... let me dig more and I'll let you know what I find
    Friday, October 4, 2019 12:06 AM
  • I found the problem , I was calling a function from another function twice....I created a stupid loop by mistake. Thanks for you help
    Friday, October 4, 2019 1:29 AM