我有一個名爲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