user979033
user979033

Reputation: 6440

How to run script and catch its return value

I have this simple function that return some value based on the passed argument:

function GetParam($fileName)
{
    if($fileName.ToLower().Contains("yes"))
    {
        return 1
    }
    elseif($fileName.ToLower().Contains("no"))
    {
        return 2
    }
    else
    {
        return -1
    }     
}

I want to call this script from another script and get its return value, in order to decide what to do next. How can I do that ?

Upvotes: 1

Views: 1213

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58931

You have to dot source the script where the GetParam function is defined to expose it to the other script:

getparam.ps1

function GetParam($fileName)
{
    if($fileName.ToLower().Contains("yes"))
    {
        return 1
    }
    elseif($fileName.ToLower().Contains("no"))
    {
        return 2
    }
    else
    {
        return -1
    }     
}

other.ps1

. .\getparam.ps1 # load the function into the current scope.
$returnValue = GetParam -fileName "yourFilename"

Note: Consider using approved Verbs and change your function name to Get-Param. Also you can omit the return keyword.

Upvotes: 1

Related Questions