允许函数 运行 在 parent 范围内?
Allow function to run in parent scope?
请原谅标题措辞有点混乱。 . .
我有一个非常简单的脚本,它只是一个 .ps1 文件的点源,但是,由于它在函数内部运行,因此不会加载到parent scope 这是我的终极 objective.
function Reload-ToolBox {
Param (
[Parameter(Mandatory=$false)]
[ValidateSet('TechToolBox',
'NetworkToolBox','All')]
[string[]]$Name = 'TechToolBox'
)
Begin
{
$ToolBoxes = @{
TechToolBox = "\path\to\my\ps1.ps1"
NetworkToolBox = "\path\to\my\ps2.ps1"
}
if ($($PSBoundParameters.Values) -contains 'All') {
$null = $ToolBoxes.Add('All',$($ToolBoxes.Values | Out-String -Stream))
}
$DotSource = {
foreach ($PS1Path in $ToolBoxes.$ToolBox)
{
. $PS1Path
}
}
}
Process
{
foreach ($ToolBox in $Name)
{
Switch -Regex ($ToolBoxes.Keys)
{
{$_ -match "^$ToolBox$"} { & $DotSource }
}
}
}
End { }
}
问题:
- 我如何才能将函数中调用的 ps1 加载到 parent 作用域中?
google 没有帮助:(
为了在内部执行点源,你的函数也对函数的调用者,您必须点源 函数调用本身 (. Reload-TooBox ...
)
不幸的是,没有办法让这个点源自动,但你至少可以检查是否 该函数是通过dot-sourcing 调用的,否则将报告错误并说明。
这是包含此检查的函数的简化版本:
function Reload-ToolBox {
[CmdletBinding()]
Param (
[ValidateSet('TechToolBox', 'NetworkToolBox', 'All')]
[string[]] $Name = 'TechToolBox'
)
Begin {
# Makes sure that *this* function is also being dot-sourced, as only
# then does dot-sourcing of scripts from inside it also take effect
# for the caller.
if ($MyInvocation.CommandOrigin -ne 'Internal') { # Not dot-sourced?
throw "You must DOT-SOURCE calls to this function: . $((Get-PSCallStack)[1].Position.Text)"
}
$ToolBoxes = @{
TechToolBox = "\path\to\my\ps1.ps1"
NetworkToolBox = "\path\to\my\ps2.ps1"
}
$ToolBoxes.All = @($ToolBoxes.Values)
if ($Name -Contains 'All') { $Name = 'All' }
}
Process {
foreach ($n in $Name) {
foreach ($script in $ToolBoxes.$n) {
Write-Verbose "Dot-sourcing $script..."
. $script
}
}
}
}
请原谅标题措辞有点混乱。 . .
我有一个非常简单的脚本,它只是一个 .ps1 文件的点源,但是,由于它在函数内部运行,因此不会加载到parent scope 这是我的终极 objective.
function Reload-ToolBox {
Param (
[Parameter(Mandatory=$false)]
[ValidateSet('TechToolBox',
'NetworkToolBox','All')]
[string[]]$Name = 'TechToolBox'
)
Begin
{
$ToolBoxes = @{
TechToolBox = "\path\to\my\ps1.ps1"
NetworkToolBox = "\path\to\my\ps2.ps1"
}
if ($($PSBoundParameters.Values) -contains 'All') {
$null = $ToolBoxes.Add('All',$($ToolBoxes.Values | Out-String -Stream))
}
$DotSource = {
foreach ($PS1Path in $ToolBoxes.$ToolBox)
{
. $PS1Path
}
}
}
Process
{
foreach ($ToolBox in $Name)
{
Switch -Regex ($ToolBoxes.Keys)
{
{$_ -match "^$ToolBox$"} { & $DotSource }
}
}
}
End { }
}
问题:
- 我如何才能将函数中调用的 ps1 加载到 parent 作用域中?
google 没有帮助:(
为了在内部执行点源,你的函数也对函数的调用者,您必须点源 函数调用本身 (
. Reload-TooBox ...
)不幸的是,没有办法让这个点源自动,但你至少可以检查是否 该函数是通过dot-sourcing 调用的,否则将报告错误并说明。
这是包含此检查的函数的简化版本:
function Reload-ToolBox {
[CmdletBinding()]
Param (
[ValidateSet('TechToolBox', 'NetworkToolBox', 'All')]
[string[]] $Name = 'TechToolBox'
)
Begin {
# Makes sure that *this* function is also being dot-sourced, as only
# then does dot-sourcing of scripts from inside it also take effect
# for the caller.
if ($MyInvocation.CommandOrigin -ne 'Internal') { # Not dot-sourced?
throw "You must DOT-SOURCE calls to this function: . $((Get-PSCallStack)[1].Position.Text)"
}
$ToolBoxes = @{
TechToolBox = "\path\to\my\ps1.ps1"
NetworkToolBox = "\path\to\my\ps2.ps1"
}
$ToolBoxes.All = @($ToolBoxes.Values)
if ($Name -Contains 'All') { $Name = 'All' }
}
Process {
foreach ($n in $Name) {
foreach ($script in $ToolBoxes.$n) {
Write-Verbose "Dot-sourcing $script..."
. $script
}
}
}
}