how can I switch pwsh function definition in a function #27046
Replies: 2 comments 3 replies
-
|
The first thing I would say is don't do this, just define the function from your export script and remove all this complexity. If you really really really want to you can access the parent session state and call Function test-function {
'abc'
$parent = (Get-Variable -Scope 1 -Name ExecutionContext -ValueOnly).SessionState
$null = $parent.InvokeProvider.Item.New(
<# path #> 'function:test-function',
<# name #> '',
<# itemTypeName #> '',
<# content #> { "def" },
<# force #> $true)
}
test-function
# abc
test-function
# defIf the function is an advanced function you have an easier way to access the parent session state through Function test-function {
[CmdletBinding()]
param ()
'abc'
$null = $PSCmdlet.SessionState.InvokeProvider.Item.New(
<# path #> 'function:test-function',
<# name #> '',
<# itemTypeName #> '',
<# content #> { "def" },
<# force #> $true)
} |
Beta Was this translation helpful? Give feedback.
-
|
In general self modifying code is considered a bad thing. The traditional approach is to have a global static where if it not set you run your initialisation , then set it once your initialisation has completed. You also have to cater for the first time run occurring from multithreaded code so need to guard the initialisation to ensure it happens just once using a mutex or critical section.. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
like it but it dot work,maybe scopes work, I can't get my error
or
The first ,
The second
Beta Was this translation helpful? Give feedback.
All reactions