Powershell 如何运行脚本并捕获其返回值

Powershell 如何运行脚本并捕获其返回值,powershell,Powershell,我有一个简单的函数,它根据传递的参数返回一些值: function GetParam($fileName) { if($fileName.ToLower().Contains("yes")) { return 1 } elseif($fileName.ToLower().Contains("no")) { return 2 } else { return -1 } }

我有一个简单的函数,它根据传递的参数返回一些值:

function GetParam($fileName)
{
    if($fileName.ToLower().Contains("yes"))
    {
        return 1
    }
    elseif($fileName.ToLower().Contains("no"))
    {
        return 2
    }
    else
    {
        return -1
    }     
}
我想从另一个脚本调用这个脚本并获取其返回值,以便决定下一步要做什么。 我如何才能做到这一点?

您必须在定义了
GetParam
函数的脚本中添加点源代码,以便将其公开给其他脚本:

getparam.ps1

function GetParam($fileName)
{
    if($fileName.ToLower().Contains("yes"))
    {
        return 1
    }
    elseif($fileName.ToLower().Contains("no"))
    {
        return 2
    }
    else
    {
        return -1
    }     
}
. .\getparam.ps1 # load the function into the current scope.
$returnValue = GetParam -fileName "yourFilename"
其他.ps1

function GetParam($fileName)
{
    if($fileName.ToLower().Contains("yes"))
    {
        return 1
    }
    elseif($fileName.ToLower().Contains("no"))
    {
        return 2
    }
    else
    {
        return -1
    }     
}
. .\getparam.ps1 # load the function into the current scope.
$returnValue = GetParam -fileName "yourFilename"

<强>注释:< /强>考虑使用和更改函数名为<代码>获取PARAM。也可以省略return关键字