0

I'm testing if I can invoke a certain function based on the condition within a foreach loop:

$BROKER = $args[0]    
$MARKETS = $args[1], $args[2], $args[3], $args[4], $args[5], $args[6]


foreach ($market in $MARKETS) {
    if ($market -like 'all') {
        &AllMarkets
        break
    }
    else {
        &CertainMarkets
    }
}


function AllMarkets {
    "Checking all markets"
}


function CertainMarkets {
    "Checking certain markets"
}

When I run the script like this:

.\script.ps1 broker all

I get this error:

& : The term 'AllMarkets' is not recognized as the name of a cmdlet, function, script file, or operable program

How to call these functions from inside the foreach loop?

1 Answer 1

2
function AllMarkets {
    "Checking all markets"
}


function CertainMarkets {
    "Checking certain markets"
}
$BROKER = $args[0]    
$MARKETS = $args[1], $args[2], $args[3], $args[4], $args[5], $args[6]


foreach ($market in $MARKETS) {
    if ($market -like 'all') {
        &AllMarkets
        break
    }
    else {
        &CertainMarkets
    }
}

The functions should be defined before the loop. Otherwise, the functions are not yet visible.

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.