Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将PowerShell函数添加到父作用域_Powershell_Powershell 2.0 - Fatal编程技术网

将PowerShell函数添加到父作用域

将PowerShell函数添加到父作用域,powershell,powershell-2.0,Powershell,Powershell 2.0,我在文件中有一些PowerShell helper函数。我想让它们在我正在编写的另一个文件的范围内可用,但不污染全局范围 $beforeFunctions = ls function: . .\helpers.ps1 $afterFunctions = ls function: $functionDiff = @(Compare-Object $beforeFunctions $afterFunctions) foreach($diffEntry in $functionDiff){ $

我在文件中有一些PowerShell helper函数。我想让它们在我正在编写的另一个文件的范围内可用,但不污染全局范围

$beforeFunctions = ls function:
. .\helpers.ps1
$afterFunctions = ls function:
$functionDiff = @(Compare-Object $beforeFunctions $afterFunctions)
foreach($diffEntry in $functionDiff){
    $func = $diffEntry.InputObject
    invoke-expression "function global:$($func.Name) { $($func.definition) }"
}
Helpers.ps1

function global:Helper1
{
    # this function pollutes the global scope
}
function Helper2
{
    # this function is not visible to the Utility.ps1 file.
}
function Helper
{
}
公用设施.ps1

&{
    ./Helpers.ps1
    function global:Utility1
    {
        Helper1
    }
    function global:Utility2
    {
        Helper2
    }
}
&{
    function global:Utility
    {
        . ./Helpers.ps1
        Helper1
    }
}
我发现这个问题: 但答案讨论了向全局范围添加函数。我真正想做的是使助手函数从一个PS1文件可用到一个正在调用的PS1文件,而不会用助手污染全局范围

我希望避免将函数定义为变量,这可以通过Set变量和-Scope参数实现。我见过的最接近的一个(从链接线程)是在函数中使用Set项:drive

任何帮助都将不胜感激

编辑:以下是从Mike的答案扩展而来的解决方案

Helpers.ps1

function global:Helper1
{
    # this function pollutes the global scope
}
function Helper2
{
    # this function is not visible to the Utility.ps1 file.
}
function Helper
{
}
公用设施.ps1

&{
    ./Helpers.ps1
    function global:Utility1
    {
        Helper1
    }
    function global:Utility2
    {
        Helper2
    }
}
&{
    function global:Utility
    {
        . ./Helpers.ps1
        Helper1
    }
}

使用点源语法加载Helpers.ps1将其内容置于实用程序函数的范围内。将Helpers.ps1放在实用程序函数之外会导致它位于&{…}范围内,但一旦定义了函数,该范围就会结束。

如果在函数中点源.ps1文件,则ps1文件中的定义不是全局的,除非函数本身是点源的。

可以在实用程序.ps1文件中使用此代码段。我们所做的是得到所有当前函数,然后我们点源助手。然后我们对前后函数进行区分。通过diff,我们在全局范围内重新创建函数

$beforeFunctions = ls function:
. .\helpers.ps1
$afterFunctions = ls function:
$functionDiff = @(Compare-Object $beforeFunctions $afterFunctions)
foreach($diffEntry in $functionDiff){
    $func = $diffEntry.InputObject
    invoke-expression "function global:$($func.Name) { $($func.definition) }"
}

我不知道网络采购。我是PS的新手。非常感谢!