Tech Off Thread

10 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Help with MSH scripts

Back to Forum: Tech Off
  • User profile image
    BrianPeiris

    Microsoft Command Shell
    Copyright (C) 2005 Microsoft Corporation. All rights reserved.

    MSH> cd "E:\Documents and Settings\brianpeiris\My Documents\My MSH Scripts"
    MSH> ls


        Directory: FileSystem::E:\Documents and Settings\brianpeiris\My Documents\My MSH Scripts


    Mode    LastWriteTime            Length Name
    ----    -------------            ------ ----
    -a---   Sep 15 16:53                478 pingAll.msh
    -a---   Sep 15 17:14                 74 say.msh


    MSH> cat say.msh
    #MSH Sample Script
    function say ($input) { "this is the output: "+$input}
    MSH> say HelloWorld
     : 'say' is not recognized as a Cmdlet, function, operable program, or script file.
    At line:1 char:4
    + say  <<<< HelloWorld
    MSH>
    Why can't I call my script?

  • User profile image
    wacko

    hmmm

  • User profile image
    Wells

    .\say

    or

    ./say

  • User profile image
    BrianPeiris

    Microsoft Command Shell
    Copyright (C) 2005 Microsoft Corporation. All rights reserved.

    MSH> cd "E:\Documents and Settings\brianpeiris\My Documents\My MSH Scripts"
    MSH> ls


        Directory: FileSystem::E:\Documents and Settings\brianpeiris\My Documents\My MSH Scripts


    Mode    LastWriteTime            Length Name
    ----    -------------            ------ ----
    -a---   Sep 15 16:53                478 pingAll.msh
    -a---   Sep 16 13:07                 48 say.msh


    MSH> cat say.msh
    #MSH Sample Script
    function say {"Hello World"}
    MSH> say
     : 'say' is not recognized as a Cmdlet, function, operable program, or script file.
    At line:1 char:3
    + say <<<<
    MSH> ./say
    MSH> .\say
    MSH> # And just to prove that the function works:
    MSH> function sayTwo {"Hello World"}
    MSH> sayTwo
    Hello World
    MSH>

    Wells> Nope, that didn't work.
    (note: I changed the function a simpler Hello World this time).

  • User profile image
    Maurits

    Try this:
    MSH> cat say.msh
    #MSH Sample Script
    function say {"Hello World"}

    OK, that proves what the function is

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

    That is just a standard error

    MSH> ./say

    That should define the function.

    NOW try

    MSH> say

    and the (now-defined) function should work and print "Hello World".

  • User profile image
    BrianPeiris

    Microsoft Command Shell
    Copyright (C) 2005 Microsoft Corporation. All rights reserved.

    MSH> cd "E:\Documents and Settings\brianpeiris\My Documents\My MSH Scripts"
    MSH> ls


        Directory: FileSystem::E:\Documents and Settings\brianpeiris\My Documents\My MSH Scripts


    Mode    LastWriteTime            Length Name
    ----    -------------            ------ ----
    -a---   Sep 15 16:53                478 pingAll.msh
    -a---   Sep 16 14:40                 50 say.msh


    MSH> cat say.msh
    #MSH Sample Script
    function say {"Hello World"}
    MSH> ./say
    MSH> say
     : 'say' is not recognized as a Cmdlet, function, operable program, or script file.
    At line:1 char:3
    + say <<<<
    MSH>

    Maurits> Nope, that didn't work either.
    P.S. does anyone know of a public (i.e. not by invite, unlike betaplace) MSH forum?

  • User profile image
    Maurits

    OK, so it looks like declaring a function on the command line loads it into the environment... but declaring a function in a script loads it only for the life of a script.  Good.

    Try adding another line - "say" - to say.msh, so it looks like this:

    MSH> cat say_file.msh
    #MSH Sample Script
    function say_function {"Hello World"}
    say_function
    MSH> ./say_file

    If this works it still leaves open the question of how you get command-line arguments into MSH variables.

    EDIT: added some text to disambiguate file names and function names

  • User profile image
    BrianPeiris

    Yeah that works, but I thinks its more of a workaround than the correct way.

  • User profile image
    Tom Servo

    The problem here is that every variable and function defined in a script is contained in the scripts scope. If the script terminates, they'll be gone.

    Easier it would be if you put say.msh in the MSH folder and put the plain function into it. If the scripts are in a trusted place, you can use them directly as function.

    Means say.msh would be plainly:

    "Hello World!"

    Better example:

    Administrator@TSN# pwd

    Path
    ----
    C:\Program Files\Microsoft Command Shell


    Administrator@TSN# gc saymore.msh
    param([string] $text = $(throw "Syntax: saymore Text"))

    "Hello World! " + $text
    Administrator@TSN# saymore
    Syntax: saymore Text
    At C:\Program Files\Microsoft Command Shell\saymore.msh:1 char:31
    + param([string] $text = $(throw <<<< "Syntax: saymore Text"))
    Administrator@TSN# saymore "More Text Here!"
    Hello World! More Text Here!
    Administrator@TSN#

    However do I not know how to add trust to My MSH Scripts. I think there's a way, but I can't remember it.

  • User profile image
    DankoD

    Try to define your function as

    function global:say( ...

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.