脚本在Powershell ISE中工作但不在PowerShell控制台中?

时间:2018-03-29 17:45:33

标签: powershell powershell-ise

脚本在Powers shell ISE中给我回复,但收到错误消息,表明HelpList未被识别为cmdlet的名称' 下面是脚本。

param($param1 = '')
if($param1 -eq '' )
{
    HelpList
}
function HelpList() 
{ 
   Write-Host "DevelopmentBuild.ps1 help"
}
function clean()
{
    Write-Host "Cleaning solution"
}

从Powershell控制台运行时,下面是错误。

PS C:\WorkSpace\Dev> .\deploymentScript.ps1
HelpList : The term 'HelpList' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\WorkSpace\Dev\deploymentScript.ps1:17 char:13
+     default{HelpList}
+             ~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (HelpList:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

2 个答案:

答案 0 :(得分:2)

PowerShell的范围规则基于所谓的"词汇范围"。必须在使用之前定义或导入任何未内置的变量或函数。在上面的脚本中,您没有在调用它时定义HelpList。重新排列代码:

param($param1 = '')

function HelpList() 
{ 
   Write-Host "DevelopmentBuild.ps1 help"
}

function clean()
{
    Write-Host "Cleaning solution"
}

if($param1 -eq '' )
{
    HelpList
}

这样,当脚本到达您调用它的点时,将定义HelpList,并且不会抛出undefined-cmdlet错误。

答案 1 :(得分:0)

尝试在代码之前定义函数:

function HelpList() 
{ 
    Write-Host "DevelopmentBuild.ps1 help"
}
function clean()
{
    Write-Host "Cleaning solution"
}

param($param1 = '')
if($param1 -eq '' )
{
    HelpList
}