2016-11-09 46 views
1

我有这个简单的函数,返回基于传入的参数一定的价值:如何运行脚本,赶上它的返回值

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

我想从另一个脚本调用此脚本,并得到它的返回值,在为了决定下一步该做什么。 我该怎么做?

回答

1

你必须点源其中GetParam函数定义将其暴露在其他脚本中的脚本:

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" 

注意:考虑使用approved Verbs并将您的函数名称更改为Get-Param。你也可以省略return关键字。

相关问题