none
Function not recognized?

    Question

  • I have a function defined at the bottom of my script that has a signature like:

    function Get-Properties($root, $nsmgr, $entityType, $modelNamespace)

    Then up above I call it like

    $propertyCollection = Get-Properties $root $nsmgr $entityType $modelNamespace

    But I am getting an error that I don't know what to do with

    Get-Properties : The term 'Get-Properties' 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:\Users\Kevinb\Script\web.ps1:28 char:25
    +   $propertyCollection = Get-Properties $root $nsmgr $entityType $modelNamespace
    +                         ~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (Get-Properties:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    I have other functions that are called but for some reason this function is not recognized.

    Ideas?


    Kevin Burton

    Friday, September 20, 2013 9:15 PM

Answers

  • They go after the param statement.  If you're using named begin, process, or end blocks in your script file, functions are generally defined in the Begin block.
    • Marked as answer by KevinBurton Sunday, September 22, 2013 10:01 PM
    Friday, September 20, 2013 10:29 PM

All replies

  • In PowerShell, functions have to be defined before they are called.  You can't place functions at the end of your file.
    Friday, September 20, 2013 9:35 PM
  • If I move all of the functions to the front of the file then the Param block is not recognized and I get the error:

    If I remove the main Param then I get an error

    Exception calling "SelectNodes" with "2" argument(s): "Namespace prefix 'x' is not defined."
    At C:\Users\Kevinb\Script\web.ps1:131 char:20
    +   foreach($node in $root.SelectNodes("//edmx:DataServices/x:Schema[@Namespace='$ ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

    That is the purpose of defining a namespace manager. 

    [System.Xml.XmlNamespaceManager]$nsmgr = $meta.NameTable
    $nsmgr.AddNamespace('x','http://schemas.microsoft.com/ado/2009/11/edm')
    $nsmgr.AddNamespace('m','http://schemas.microsoft.com/ado/2007/08/dataservices/metadata')
    $nsmgr.AddNamespace('edmx','http://schemas.microsoft.com/ado/2007/06/edmx')

    If I call this function ($root.SelectNodes("//edmx:DataServices/x:Schema[@Namespace='Default']/x:EntityContainer/x:EntitySet", $nsmgr)) directly from the debugger it seems to work.

    It seems that something is messed up when I move the functions.


    Kevin Burton

    Friday, September 20, 2013 10:11 PM
  • They go after the param statement.  If you're using named begin, process, or end blocks in your script file, functions are generally defined in the Begin block.
    • Marked as answer by KevinBurton Sunday, September 22, 2013 10:01 PM
    Friday, September 20, 2013 10:29 PM
  • Hi Kevin,

    you can actually have your main program above your function definitions if you dot source your main function at the bottom of the script like so:

    function main{
      test
      test2
    }
    
    function test{
      "something"
    }
    
    function test2{
      "something else"
    }
    #dot source main here 
    . main
    

    Saturday, September 21, 2013 8:19 PM
  • That's interesting.  I didn't know you could dot-source a function; I'd only done that when calling other scripts.
    Sunday, September 22, 2013 1:52 AM