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?
-
-
.\say
or
./say
-
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). -
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".
-
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?
-
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 -
Yeah that works, but I thinks its more of a workaround than the correct way.
-
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. -
Try to define your function as
function global:say( ...
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.