从导入的PowerShell模块访问变量
我有很多脚本,希望在其中使用一组公共变量,因此我创建了一个名为CommonVariables.psm1的文件,在其中初始化变量。此文件与需要使用它的脚本位于同一文件夹中 在其他脚本的开头,我执行这两行代码,试图导入并使用CommonVariables.psm1中的变量从导入的PowerShell模块访问变量,powershell,Powershell,我有很多脚本,希望在其中使用一组公共变量,因此我创建了一个名为CommonVariables.psm1的文件,在其中初始化变量。此文件与需要使用它的脚本位于同一文件夹中 在其他脚本的开头,我执行这两行代码,试图导入并使用CommonVariables.psm1中的变量 $myHome = resolve-path (Split-Path -Parent $PSCommandPath) Import-Module -Name (Join-Path $myHome -ChildPath "Commo
$myHome = resolve-path (Split-Path -Parent $PSCommandPath)
Import-Module -Name (Join-Path $myHome -ChildPath "CommonVariables.psm1") -Verbose
这看起来很好,但是变量没有在调用脚本中初始化
为了测试这一点,我在CommonVariables.psm1的末尾放了一个this代码
Write-Host ("1-" + $someVar)
输出与预期一样,其中$someVar具有初始化值
脚本中导入CommonVariables.psm1文件和$someVar的类似Write Host语句为null
如何导入CommonVariables.psm1文件,然后使用其中的初始化变量?我想我只需要在变量前面加上$Global:someVar。您可以使用
导入模块上的-Global
标志,但同样,作为.psd1
和导入PowerShellDataFile
我也将文件从CommonVariables.psm1命名为CommonVariable.ps1,然后使用带有-Global标志的导入模块。我想我只需要在变量前面加上$Global:someVar。您可以使用-Global
标志在Import Module
上,但同样,作为.psd1
和Import PowerShellDataFile
我也将文件从CommonVariables.psm1命名为CommonVariable.ps1,然后使用带有-Global标志的Import Module