> On Wed, 16 May 2007 07:12:01 -0700, Dabbler wrote:
>
> > Thanks Jeffery, that's what I was looking for. I think your latter solution
> > of using global scope makes the most sense. The ./foo.ps1 then approximates
> > "include" in other languages.
> >
> > "Jeffery Hicks" <"jhicks[at]SAPIEN.com" wrote:
> >
> >> On Tue, 15 May 2007 20:59:00 -0700, Dabbler wrote:
> >>
> >>> How do I reuse a function in one script file, say foo.ps1 from another script
> >>> file say main.ps1?
> >>>
> >>> Confused about modular programming with PowerShell !
> >>>
> >>> Thanks much.
> >>
> >> The quick and easy way is to put the functions in your profile. Otherwise
> >> you have scope issues where the functions from one script aren't available
> >> to the other script. You can certainly call one script from another. This
> >> works:
> >>
> >> #foo.ps1
> >> Function alpha {
> >> Write-Host "This is function alpha in foo.ps1"
> >> }
> >>
> >> alpha
> >> #end foo.ps1
> >>
> >> #bar.ps1
> >> Write-Host "Running bar.ps1"
> >>
> >> ..\foo.ps1
> >> #end bar.ps1
> >>
> >> When I execute bar.ps1, the script foo.ps1 is executed and the function
> >> within it.
> >>
> >> Otherwise, you need to set the scope of the function in foo.ps1 to global:
> >> #foo.ps1
> >> Function global:alpha {
> >> Write-Host "This is function alpha in foo.ps1"
> >> }
> >> #end foo.ps1
> >>
> >> Now bar.ps1 can look like this:
> >> #bar.ps1
> >> Write-Host "Running bar.ps1"
> >> ..\foo.ps1
> >> alpha
> >> #end bar.ps1
> >>
> >> When bar.ps1 is executed the alpha function is available. And it will be
> >> available in the shell because it now has a global scope.
> >>
> >>
> >>
> >> --
> >> Jeffery Hicks
> >> SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
> >> VBScript & Windows PowerShell Training -
> >> www.ScriptingTraining.com/classes.asp
> >> Windows PowerShell? - www.SAPIENPress.com/powershell.asp
> >>
> >> blog:
http://blog.SAPIEN.com> >> blog:
http://jdhitsolutions.blogspot.com> >>
>
> I put alot of the functions I use in my profile so they are always
> available.
>
> --
> Jeffery Hicks
> SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
> VBScript & Windows PowerShell Training -
> www.ScriptingTraining.com/classes.asp
> Windows PowerShell? - www.SAPIENPress.com/powershell.asp
>
> blog:
http://blog.SAPIEN.com> blog:
http://jdhitsolutions.blogspot.com>