0

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
3
  • 2
    PowerShell runs logically in the order of the commands received. You're trying to call the function before it's actually declared. This is why testing in the ISE is a bad practice because the scopes are polluted. Commented Mar 29, 2018 at 17:49
  • But its running in PowerShell ISE. Commented Mar 29, 2018 at 17:58
  • There are some subtle differences in the way scopes are handled by the ISE vs. the console. The console rules are stricter, and does not retain definitions that the ISE does. Commented Mar 29, 2018 at 18:07

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@nitiparikh: If Jeff's solution addressed your issue successfully, you might want to accept that as a solution. It helps other users in the future who would encounter similar issues!
0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.