[MSH]scripted function could not start from file in MSH

Discussion in 'Scripting' started by Joerg Wiedmann, Feb 14, 2006.

  1. Hello,

    i have written a function and saved as myfunc.msh. In the file myfunc.msh i
    have defined an alias for my function.
    Now i would use this function in msh, but it does not work.

    When i script the function directly in the Microsoft Shell it works fine.

    What goes wrong?
    Has anyone an idee?
    Thanks for helping.
     
    Joerg Wiedmann, Feb 14, 2006
    #1
    1. Advertisements

  2. Joerg Wiedmann

    RichS Guest

    Did you run the script in process i.e. as

    .. myfunc.msh

    that should make the function available to the shell
     
    RichS, Feb 14, 2006
    #2
    1. Advertisements

  3. Hello,

    i will show show you my example:

    this is myfunc.msh:
    function text_files
    {
    get-childitem c:\*.txt
    }

    Now i do for calling myfunc.msh:
    set-location where myfunc.msh is.
    Then i type on the prompt: ./myfunc.msh

    then i will test my function with typing text_files

    And it does not work with message:

    'text_files' is not recognized as a Cmdlet, function, operable program, or
    script file.
    At line:1 char:10
    + text_files <<<<

    What is wrong?
    Thanks for helping
     
    Jörg Wiedmann, Feb 14, 2006
    #3
  4. Dot sourcing MSH to load the child scope into the parent scope when the
    script is complete
    MSH C:\monad> . myfunc.msh
    MSH C:\monad> text_files


    Directory: Microsoft.Management.Automation.Core\FileSystem::C:\


    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    -a--- 1/14/2004 11:40 PM 837 avi_log.txt


    In your script , the function has the "script" scope, when the script exits,
    the function disappears as well.
    You need dot sourcing to load the function into the "global" scope.

    --
    Wei Wu [MSFT]
    MSH Team
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, and confers no rights.

     
    Wei Wu [MSFT], Feb 14, 2006
    #4
  5. A few small clarifications on this thread. Dot-sourcing a script (or
    ScriptBlock) means that the script will run in the current scope. We do not
    start up new processes for scripts as that would prevent us from sharing
    live objects between scripts and the interactive shell. We also do not
    "load" the child scope into the parent scope when the scripts completes.
    When dot-sourcing we just don't create the child scope to begin with.

    --
    Jeff Jones [MSFT]
    Monad Development
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, and confers no rights.



     
    Jeff Jones [MSFT], Feb 14, 2006
    #5
  6. Joerg Wiedmann

    forestial Guest

    I'm having the same problem as the thread starter, but even using these
    replies I still can't get dot sourcing to work:

    MSH:265> .myfunc.msh
    'myfunc.msh' is not recognized as a Cmdlet, function, operable program, or
    script file.
    At line:1 char:3
    + . <<<< myfunc.msh

    In case I am getting confused by the formatting of posts in this forum,
    that's a dot and the filename immediately after the prompt, with no spaces.

    I have also tried it with a space between the dot and the filename, same
    result.
     
    forestial, Feb 14, 2006
    #6
  7. there are 2 things to consider

    1) files in the current directory need a path to start

    so you can not do
    myfunc.msh
    but need to give it a path :
    ..\myfunc.msh

    (in this case a relative path using . as current directory)

    2) if you need to load the variables / functions of the script in the
    $global - scope ( the scope of the interactive session )
    you need to "dot-source" it (using a dot and a space)

    in this case you need to do both, so it gets :

    .. .\myfunc.msh

    the first dot to get in in the current scope, the second because it's in
    the current directory.

    hope this helps clearing it up

    gr /\/\o\/\/
     
    /\\/\\o\\/\\/, Feb 14, 2006
    #7
  8. Thanks a lot :

    .. .\myfunc.msh

    now it works.
     
    Jörg Wiedmann, Feb 15, 2006
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.