powershell break is stopping entire script

Viewed 41

When one of the 'break' commands below is activated it stops the entire script. There is another function with 'break's in it and it runs fine and is directly followed by this function which stops the execution of the rest of the script.

Function GetNameRequest {
    $EMLpath = @(gci $global:WTCHfldr\* -name)
    $EMLpath = $EMLpath[0]
    $global:EMLpath = "$global:WTCHfldr\$EMLpath"
    $n = select-string -Path "$global:EMLpath\*.eml" -Pattern "[NameRequest]" -SimpleMatch -CaseSensitive -Context 0, 20
    $GotItN = $false
    $n.Context.PostContext |
    ForEach-Object {
        if ($_ -match "View order:") {
            $GotItN = $true
        }
    }
    
    if ($GotItN) {
        $NMRQ1 = ($n.Context.PostContext[0] -replace '[\W]', '')
        $NMRQ2 = ($n.Context.PostContext[1] -replace '[\W]', '')
        $NMRQ3 = ($n.Context.PostContext[2] -replace '[\W]', '')
        $SRCHrslt1 = (get-aduser -Filter * | select samAccountName) | Where-Object { $_.'samAccountName' -eq "$NMRQ1" }
        $NMRQ1
        if ($null -eq $SRCHrslt1) { $global:USRNME = $NMRQ1; break }
        $SRCHrslt2 = (get-aduser -Filter * | select samAccountName) | Where-Object { $_.'samAccountName' -eq "$NMRQ2" }
        $NMRQ2
        if ($null -eq $SRCHrslt2) { $global:USRNME = $NMRQ2; break }
        $SRCHrslt3 = (get-aduser -Filter * | select samAccountName) | Where-Object { $_.'samAccountName' -eq "$NMRQ3" }
        $NMRQ3
        if ($null -eq $SRCHrslt3) { $global:USRNME = $NMRQ3; break }
    }
}
0 Answers
Related