Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: Windows Server » microsoft.public.windows.server.scripting
Thread: function reuse

HTVi
TV Discussion Newsgroups

function reuse
Dabbler 5/16/2007 3:59:00 AM
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.

Re: function reuse
Jeffery Hicks <"jhicks[at]SAPIEN.com"> 5/16/2007 1:46:58 PM
On Tue, 15 May 2007 20:59:00 -0700, Dabbler wrote:

[Quoted Text]
> 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
Re: function reuse
Dabbler 5/16/2007 2:12:01 PM
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:

[Quoted Text]
> 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
>
Re: function reuse
Jeffery Hicks <"jhicks[at]SAPIEN.com"> 5/16/2007 2:54:46 PM
On Wed, 16 May 2007 07:12:01 -0700, Dabbler wrote:

[Quoted Text]
> 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
Re: function reuse
Dabbler 5/17/2007 9:58:01 AM
I'm still bothered by this. Having been raised on modular programming
methodology it seems "unfair" that I can't create modular script files and
call them without having to convert them to global functions and preload them.

I found a case in point on PowerGadget website, a script called gpo.ps1.

http://powergadgets.com/csPg/forums/permalink/413/284/ShowThread.aspx#284

It's purpose is to run an external command passed in as a parameter and
execute it while hiding the command window flash. I can't figure out how they
are able to call it, as I get the cmdlet not found error when I use it. The
file doesn't contain a function so I can't pre-load the function into my
workspace by using ./gpo.ps1.

Any thoughts on this?

"Jeffery Hicks" <"jhicks[at]SAPIEN.com" wrote:

[Quoted Text]
> 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
>

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen