|
|
#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
|
|
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 #####
|
|
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.
|
|
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
|
|
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 > >
|
|
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
|
|
[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!
|
|
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 }
|
|
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
|
|
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.
|
|
|