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: General » microsoft.public.windows.powershell
Thread: How to make the result of function GLOBAL?

HTVi
TV Discussion Newsgroups

How to make the result of function GLOBAL?
applepwc 12/25/2008 4:18:02 PM
#1,place a Test-LeapYear.ps1 file in current ps path
#2,import-function .\Test-LeapYear.ps1
#3,"Test-LeapYear 2008" will not work

function import-function($pathname)
{
$funName = (Get-Item $pathname).name.split(".")[0]
$funBody = get-content $pathname | out-string
$fun = "function "
$fun += $funName
$fun += "(){`n"
$fun += $funBody
$fun += "`n }"

invoke-Expression $($fun | out-string)
#Test-LeapYear 2008
$fun
}

but:
PS 36> invoke-expression $(import-function .\Test-LeapYear.ps1)
PS 37> Test-LeapYear 2008
True


###############
I can't find the way to solve it.
Any suggestion will be appreciated.
--
greeting

applepwc
Re: How to make the result of function GLOBAL?
tojo2000 <tojo2000[ at ]gmail.com> 12/25/2008 6:28:55 PM
On Dec 25, 9:18 am, applepwc <apple...[ at ]discussions.microsoft.com>
wrote:
[Quoted Text]
> #1,place a Test-LeapYear.ps1 file in current ps path
> #2,import-function .\Test-LeapYear.ps1
> #3,"Test-LeapYear 2008" will not work
>
> function import-function($pathname)
> {
>         $funName = (Get-Item $pathname).name.split(".")[0]
>         $funBody = get-content $pathname | out-string
>         $fun = "function "
>         $fun += $funName
>         $fun +=  "(){`n"
>         $fun += $funBody
>         $fun += "`n }"
>
>         invoke-Expression $($fun | out-string)
>         #Test-LeapYear 2008
>         $fun
>
> }
>
> but:
> PS 36> invoke-expression $(import-function .\Test-LeapYear.ps1)
> PS 37> Test-LeapYear 2008
> True
>
> ###############
> I can't find the way to solve it.
> Any suggestion will be appreciated.
> --
> greeting
>
> applepwc

It sounds like what you're looking for is functionality known as
dotsourcing. You use the dot (.) operator, and it will suck in a
script as if it was typed in right there at the prompt. See the
example below:

##### test.ps1 #####
function Say-Hello {
echo 'Hello World!'
}
##### test.ps1 #####

C:\Users\tojo2000\Documents> .\test.ps1
C:\Users\tojo2000\Documents> Say-Hello
The term 'Say-Hello' is not recognized as a cmdlet, function,
operable program, or script file. Verify the term and try again.
At line:1 char:10
+ Say-Hello <<<<
C:\Users\tojo2000\Documents> . .\test.ps1
C:\Users\tojo2000\Documents> Say-Hello
Hello World!

Now that doesn't necessarily make it global, but it makes all
functions and variables created within the script available in your
current scope. If you specifically wanted them to be available
globally, you would have to declare them as global in the script:

##### test2.ps1 #####
function global:Say-Hello {
echo 'Hello World!'
}
$global:myvariable = 1
##### test2.ps1 #####
Re: How to make the result of function GLOBAL?
tojo2000 <tojo2000[ at ]gmail.com> 12/25/2008 6:38:49 PM
On Dec 25, 11:28 am, tojo2000 <tojo2...[ at ]gmail.com> wrote:
[Quoted Text]
> On Dec 25, 9:18 am, applepwc <apple...[ at ]discussions.microsoft.com>
> wrote:
>
>
>
> > #1,place a Test-LeapYear.ps1 file in current ps path
> > #2,import-function .\Test-LeapYear.ps1
> > #3,"Test-LeapYear 2008" will not work
>
> > function import-function($pathname)
> > {
> >         $funName = (Get-Item $pathname).name.split(".")[0]
> >         $funBody = get-content $pathname | out-string
> >         $fun = "function "
> >         $fun += $funName
> >         $fun +=  "(){`n"
> >         $fun += $funBody
> >         $fun += "`n }"
>
> >         invoke-Expression $($fun | out-string)
> >         #Test-LeapYear 2008
> >         $fun
>
> > }
>
> > but:
> > PS 36> invoke-expression $(import-function .\Test-LeapYear.ps1)
> > PS 37> Test-LeapYear 2008
> > True
>
> > ###############
> > I can't find the way to solve it.
> > Any suggestion will be appreciated.
> > --
> > greeting
>
> > applepwc
>
> It sounds like what you're looking for is functionality known as
> dotsourcing.  You use the dot (.) operator, and it will suck in a
> script as if it was typed in right there at the prompt.  See the
> example below:
>
>   ##### test.ps1 #####
>   function Say-Hello {
>     echo 'Hello World!'
>   }
>   ##### test.ps1 #####
>
>   C:\Users\tojo2000\Documents>  .\test.ps1
>   C:\Users\tojo2000\Documents>  Say-Hello
>   The term 'Say-Hello' is not recognized as a cmdlet, function,
> operable program, or script file. Verify the term and try again.
>   At line:1 char:10
>   + Say-Hello <<<<
>   C:\Users\tojo2000\Documents>  . .\test.ps1
>   C:\Users\tojo2000\Documents>  Say-Hello
>   Hello World!
>
> Now that doesn't necessarily make it global, but it makes all
> functions and variables created within the script available in your
> current scope.  If you specifically wanted them to be available
> globally, you would have to declare them as global in the script:
>
>   ##### test2.ps1 #####
>   function global:Say-Hello {
>     echo 'Hello World!'
>   }
>   $global:myvariable = 1
>   ##### test2.ps1 #####

Oh, BTW, note that there's a space between the dots, so it's . .
\test.ps1, not ..\test.ps1.
Re: How to make the result of function GLOBAL?
applepwc 12/26/2008 11:06:03 AM
hello,tojo2000:
May be I didn't say clear.
My ps1 file didn't include "Function" term.
Look at this:

PS 1> get-content say-hello.ps1
echo 'Hello World!'
PS 2> . .\say-hello.ps1
Hello World!
PS 3> say-hello
The term 'say-hello' is not rec...

#==============
you can download this file,and try it you will konw what I mean: http://blogs.msdn.com/powershell/archive/2008/12/23/advanced-functions-and-test-leapyear-ps1.aspx
#==============
And I have read about_scope file before I post the quesion.

Thanks.

--
greeting

applepwc


Re: How to make the result of function GLOBAL?
"Vadims Podans" <vpodans> 12/26/2008 1:15:01 PM
what you want to get in the end? I not so clearly understand your problem.

"applepwc" <applepwc[ at ]discussions.microsoft.com> rakstīja ziņojumā
"news:B1714C63-5018-40D3-8B5D-B4767823E555[ at ]microsoft.com"...
[Quoted Text]
> hello,tojo2000:
> May be I didn't say clear.
> My ps1 file didn't include "Function" term.
> Look at this:
>
> PS 1> get-content say-hello.ps1
> echo 'Hello World!'
> PS 2> . .\say-hello.ps1
> Hello World!
> PS 3> say-hello
> The term 'say-hello' is not rec...
>
> #==============
> you can download this file,and try it you will konw what I mean:
> http://blogs.msdn.com/powershell/archive/2008/12/23/advanced-functions-and-test-leapyear-ps1.aspx
> #==============
> And I have read about_scope file before I post the quesion.
>
> Thanks.
>
> --
> greeting
>
> applepwc
>
>
Re: How to make the result of function GLOBAL?
tojo2000 <tojo2000[ at ]gmail.com> 12/26/2008 3:07:29 PM
On Dec 26, 4:06 am, applepwc <apple...[ at ]discussions.microsoft.com>
wrote:
[Quoted Text]
> hello,tojo2000:
>    May be I didn't say clear.
>    My ps1 file didn't include "Function" term.
>    Look at this:
>
> PS 1> get-content say-hello.ps1
> echo 'Hello World!'
> PS 2> . .\say-hello.ps1
> Hello World!
> PS 3> say-hello
> The term 'say-hello' is not rec...
>
> #==============
> you can download this file,and try it you will konw what I mean:http://blogs.msdn.com/powershell/archive/2008/12/23/advanced-function...
> #==============
> And I have read about_scope file before I post the quesion.
>
> Thanks.
>
> --
> greeting
>
> applepwc

Okay, this example is different because you are just executing the
main function of a script. Just call the script instead of trying to
call it as a function:

PS 1> .\Test-LeapYear.ps1 2008

Re: How to make the result of function GLOBAL?
applepwc[ at ]gmail.com 12/26/2008 3:18:37 PM

[Quoted Text]
> what you want to get in the end? I not so clearly understand your problem.
I'm sorry for my misunderstanding

My problem is here:

1,download the script:
http://blogs.msdn.com/powershell/archive/2008/12/23/more-advanced-functions-get-assembly-and-get-exportedtype.aspx

2,How can you make Get-ExportedType worked without modified the
original script file?

3,you will konw what I want and why I make a import-function.

thanks!

and to tojo2000:
>PS 1> .\Test-LeapYear.ps1 2008
I want to type "Test-LeapYear" in my cmd shell and it work.
So I make the import-function function.

thanks!
Re: How to make the result of function GLOBAL?
tojo2000 <tojo2000[ at ]gmail.com> 12/26/2008 5:12:35 PM
On Dec 26, 8:18 am, apple...[ at ]gmail.com wrote:
[Quoted Text]
> > what you want to get in the end? I not so clearly understand your problem.
>
> I'm sorry for my misunderstanding
>
> My problem is here:
>
> 1,download the script:http://blogs.msdn.com/powershell/archive/2008/12/23/more-advanced-fun...
>
> 2,How can you make Get-ExportedType worked without modified the
> original script file?
>
> 3,you will konw what I want and why I  make a import-function.
>
> thanks!
>
> and to tojo2000:>PS 1> .\Test-LeapYear.ps1 2008
>
> I want to type "Test-LeapYear" in my cmd shell and it work.
> So I make the  import-function function.
>
> thanks!

Okay, I think I see what you mean. You could probably do this simpler
with an alias instead of a function:

function Import-Script {
param([string]$file_path)

if (-not (Test-Path $file_path)) {
Write-Error "File $file_path not found!"
}

$file_obj = Get-Item $file_path
Set-Alias -scope 'Global' $file_obj.basename $file_obj.fullname
}



RE: How to make the result of function GLOBAL?
Kiron 12/26/2008 6:24:00 PM
The problem is that Invoke-Expression is executed in import-function's scope
and the new function exists only in that scope. To solve this prepend
'global:' to the new functtion's name:

<#
basename is available in v2 CTP*
Note that the last statement is one pipeline
#>
function import-function ($pathname) {
$funName, $scriptPath =
Get-Item $pathname | % {$_.basename, $_.fullname}
get-content $scriptPath | out-string |
invoke-expression -command {"function global:$funName {$_}"}
}

import-function .\Test-LeapYear.ps1
#Test-LeapYear
Test-LeapYear 2008

--
Kiron
Re: How to make the result of function GLOBAL?
applepwc[ at ]gmail.com 12/27/2008 3:18:29 AM
To Kiron:
yeah,this is what I want exactly.Thanks.
I'm fool to try to put "global:" in front of invoke-Expression.
ps:the whole script is very beautiful,how concise it is.

To tojo2000:
Another method.Thanks.
I think your method will save more memory and provide clean powershell
execution envrionment than mine.
If I have thousand of functions to import.

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