Mit Jacob
Mit Jacob

Reputation: 195

Script Works in Powershell ISE But not In PowerShell Console?

Script giving me response in Powers shell ISE but getting Error message that HelpList is not recognized as the name of a cmdlet' Below is script.

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

Below is Error when running from Powershell console.

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

Upvotes: 0

Views: 1389

Answers (2)

Jeff Zeitlin
Jeff Zeitlin

Reputation: 10809

PowerShell's scope rules are based on what is called "lexical scoping". Any variable or function that is not built in must be defined or imported before being used. In your script above, you have not defined HelpList at the point that you call it. Rearrange your code:

param($param1 = '')

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

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

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

This way, HelpList will be defined when the script reaches the point you call it, and will not throw the undefined-cmdlet error.

Upvotes: 3

Tripp Kinetics
Tripp Kinetics

Reputation: 5459

Try defining the functions before the code:

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

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

Upvotes: 0

Related Questions