定义模块中的功能范围

时间:2017-06-06 13:41:39

标签: powershell scope

我们有以下两个文件:

脚本A

$VerbosePreference = 'Continue'

Import-ParamsTestHC -source 'Bananas'

Write-Verbose 'Script A needs Bananas:'
$EventParams

&'S:\Test\Brecht\Testie5.ps1'

Write-Verbose 'Script A needs Bananas:'
$EventParams

脚本B:

Write-Verbose 'Script B needs Apples'

Import-ParamsTestHC -source 'Apples'
$EventParams

带功能的模块文件:

Function Import-ParamsTestHC {
    [CmdLetBinding()]
    Param (
        [Parameter(Mandatory)]
        [String]$Source
    )

    $Global:EventParams = @{
        Fruit = $Source
    }
}

输出:

VERBOSE: Script A needs Bananas:

Name                           Value                                                                                       
----                           -----                                                                                       
Fruit                          Bananas                                                                                     
VERBOSE: Script B needs Apples
Fruit                          Apples                                                                                      
VERBOSE: Script A needs Bananas:
Fruit                          Apples   

期望的输出:

VERBOSE: Script A needs Bananas:

Name                           Value                                                                                       
----                           -----                                                                                       
Fruit                          Bananas                                                                                     
VERBOSE: Script B needs Apples
Fruit                          Apples                                                                                      
VERBOSE: Script A needs Bananas:
Fruit                          Bananas  

我们正在尝试导入两个脚本中的splatting参数。但似乎很难有一个特定于该脚本的范围。有没有解决的办法?

当我们尝试将全局范围$Global:EventParams更改为脚本范围$Script:EventParams时,变量完全不可用。

0 个答案:

没有答案