我有一个名为Test.psm1的PowerShell模块。我想为变量设置一个值,并且在我调用该模块中的另一个方法时可以访问它。在PowerShell模块中设置属性
#Test.psm1
$property = 'Default Value'
function Set-Property([string]$Value)
{
$property = $Value
}
function Get-Property
{
Write-Host $property
}
Export-ModuleMember -Function Set-Property
Export-ModuleMember -Function Get-Property
从PS命令行:
Import-Module Test
Set-Property "New Value"
Get-Property
在这一点上我想它返回“新价值”,但它的返回“默认值”。我试图找到一种方法来设置该变量的范围,但没有任何运气。
+1模块有其自己的范围,可以将模块与来自呼叫者环境的意外污染隔离开来。 – JPBlanc 2012-04-11 04:12:54