• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How can I tell if a function is already defined?

K

Kevin Buchan

#1
Maybe I'm looking at this wrong, but what I want to do is
conditionally dot source a .ps1 file if it hasn't already been loaded
earlier in my script.

I'm making use of the "Should-Process.ps1" script so that I can add
that great functionality to my own special purpose functions. Each of
these is currently maintained in separate scripts and each has a
dependency on the Should-Process.ps1 script. I'd like to just add a
line to dot source the Should-Process.ps1 file if the Should-Process
function is not already defined.

I can't seem to figure out how to tell if it is, though.

Thanks.

--
Kevin Buchan
 
B

Brandon Shell [MVP]

#2
get-content function:<Name>

Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

K> Maybe I'm looking at this wrong, but what I want to do is
K> conditionally dot source a .ps1 file if it hasn't already been loaded
K> earlier in my script.
K>
K> I'm making use of the "Should-Process.ps1" script so that I can add
K> that great functionality to my own special purpose functions. Each
K> of these is currently maintained in separate scripts and each has a
K> dependency on the Should-Process.ps1 script. I'd like to just add a
K> line to dot source the Should-Process.ps1 file if the Should-Process
K> function is not already defined.
K>
K> I can't seem to figure out how to tell if it is, though.
K>
K> Thanks.
K>
 
J
#3
if (!(test-path function:\should-process)) {. Should-Process.ps1}

should do it.

You'd need to specify the full path to Should-Process.ps1, if it's not in
your path.

--
Jon


"Kevin Buchan" <kevin.buchan@xxxxxx[Pls Don't Spam]sanders.com> wrote in
message news:ml7hq39trfk79j49sjq66gdkoeefbmgfsp@xxxxxx

> Maybe I'm looking at this wrong, but what I want to do is
> conditionally dot source a .ps1 file if it hasn't already been loaded
> earlier in my script.
>
> I'm making use of the "Should-Process.ps1" script so that I can add
> that great functionality to my own special purpose functions. Each of
> these is currently maintained in separate scripts and each has a
> dependency on the Should-Process.ps1 script. I'd like to just add a
> line to dot source the Should-Process.ps1 file if the Should-Process
> function is not already defined.
>
> I can't seem to figure out how to tell if it is, though.
>
> Thanks.
>
> --
> Kevin Buchan
 
K

Kevin Buchan

#4
That's perfect!

I knew that there had to be a great solution like this, but never
considered using 'Test-Path'.

-Kevin

On Tue, 5 Feb 2008 17:54:36 -0000, "Jon"
<Email_Address@xxxxxx> wrote:

>
>if (!(test-path function:\should-process)) {. Should-Process.ps1}
>
>should do it.
>
>You'd need to specify the full path to Should-Process.ps1, if it's not in
>your path.