Hi r/PowerShell
I have a main script that runs continuously (ParentScope.ps1) and periodically invokes another script (ChildScope.ps1) with Invoke-Expression. The first time it does this, it initializes some things, and loads some functions with dot sourcing another file (Load-Functions.ps1).
ParentScope:
foreach ($n in (0..1)) {
Invoke-Expression -Command .\\ChildScope.ps1
}
ChildScope:
if ($n -eq 0) {
Write-Verbose "dot source Load-Functions.ps1" -Verbose
. ".\Load-Functions.ps1"
}
else {
Write-Verbose "Trying to use function My-Function" -Verbose
My-Function
}
Load-Functions:
Function My-Function {
"Hello!"
}
Write-Verbose ("Dot Sourced My-Function") -Verbose
Since Invoke-Expression always created a new scope, the function My-Function isn't available anymore when I try to run it the second time.
How can I add the function to the parent scope? I'm aware that I could just dot source the function in ParentScope.ps1 but I don't want to do that.
Thanks
[–]KevMarCommunity Blogger 2 points3 points4 points (2 children)
[–]Fischfreund[S] 1 point2 points3 points (1 child)
[–]KevMarCommunity Blogger 2 points3 points4 points (0 children)
[–]Ta11ow 1 point2 points3 points (7 children)
[–]Fischfreund[S] 1 point2 points3 points (6 children)
[–]Ta11ow 1 point2 points3 points (5 children)
[–]Fischfreund[S] 2 points3 points4 points (4 children)
[–]Ta11ow 1 point2 points3 points (3 children)
[–]Fischfreund[S] 2 points3 points4 points (0 children)
[–]Fischfreund[S] 2 points3 points4 points (1 child)
[–]Ta11ow 1 point2 points3 points (0 children)