Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Defining global scoped functions from within a script file

2 views
Skip to first unread message

Al Fansome

unread,
Nov 21, 2009, 6:48:20 PM11/21/09
to
I want to have a define.ps1 file that defines a set of functions that
will end up in the function: drive, similar to the $profile file that
runs at startup. The functions are defined in the normal manner, as they
would be in $profile.

If I execute this file as

.\define

the functions are defined only in the scope of the script, and so are
gone when the script finishes. If I execute the file as

. .\define

they are inserted into the function: drive, as the initial ". " allows
the script to affect execution environment.

I am interested in whether there is a simple way to do this without
using the ". " modifier on the command line, similar to using the
$global: modifier to affect the scope of variables. I realize that the
effort to type ". " before the script name is trivial; this is just for
my own curiosity.

Thanks,

Al

Marco Shaw [MVP]

unread,
Nov 21, 2009, 7:35:18 PM11/21/09
to
> I am interested in whether there is a simple way to do this without using
> the ". " modifier on the command line, similar to using the $global:
> modifier to affect the scope of variables. I realize that the effort to
> type ". " before the script name is trivial; this is just for my own
> curiosity.

If you don't want to dot-source, then you have to use the $global: modifier
in the script.

AFAIK, there aren't any other options...

Marco

Martin Zugec

unread,
Nov 22, 2009, 7:46:54 AM11/22/09
to
Hi,

Definitely most reliable way is to use
Function Global:MyFunction {}

Martin

"Al Fansome" <al_fa...@hotmail.com> wrote in message
news:oJOdnTzlZLrZ4ZXW...@supernews.com...

Thomas Lee

unread,
Nov 22, 2009, 8:11:30 AM11/22/09
to
In message <oJOdnTzlZLrZ4ZXW...@supernews.com>, Al Fansome
<al_fa...@hotmail.com> writes

>I am interested in whether there is a simple way to do this without
>using the ". " modifier on the command line, similar to using the
>$global: modifier to affect the scope of variables. I realize that the
>effort to type ". " before the script name is trivial; this is just for
>my own curiosity.

As an alternative, why not just define these functions in a module, then
add that module to the profile (Or manually add it at run time). It's
not quite the answer you were looking for, but IMHO, possibly better.

HTH

Thomas
--
Thomas Lee
doct...@gmail.com

0 new messages