I have a function that includes another scripts:
function include-function($fileName)
{
.$fileName
}
I store this function in another script
From my main script I want to first include this script and then include another script:
."c:\1.ps1" #include first file
include-function "c:\2.ps1" #call function to include other functions
xtest "bbb" #function from 2.ps1 that should be included
The problem is that function xtest from 2.ps1 is not visible in main script, it’s only visible in scope of include-function. Is there a way to pass xtest to main script?
My include function doesn’t realy load file (it gets it as string from API), so I can’t call it directly from main script. As a workaround I just changed include-function to return me contents of a file and then from main script I call Invoke-Expression (include-function “c:\2.ps1”)
Thanks
The explanation is the scope of your vars and function if in
2.ps1, you declare your vars and functions as globals they will be visible in the global scope.Exemple of 2.ps1 :
usage
test.ps1:gives :
As you tag your question in PowerShell V2.0 you’d better have a look to modules. using module will end in a best structured programmation see about_Modules.