The 我使用的腳本Microsoft.PowerShell_profile.ps1運行時會創建很多變量。我已將所有變量的範圍設置爲「腳本」,但腳本中使用的變量永遠不會超出範圍。Powershell Profile腳本點源?
我希望一旦腳本完成運行並將控制權交給我後,變量就會超出範圍。
如果我比較了全局變量,本地變量和腳本變量的數量,我想出了相同的數字。
例子:
# Profile script does what it does.
Get-Variable -Scope Global | Measure-Object
Get-Variable -Scope Local | Measure-Object
Get-Variable -Scope Script | Measure-Object
Output:
60
60
60
目前,我捕捉變量的快照我的配置腳本的開頭,然後在最後刪除任何新的變數。
例子:
$snapshotBefore = Get-Variable
$profileVar1 = 'some value'
$profileVar2 = 'some other value'
$snapshotAfter = Get-Variable
# Compare before and after, and create list of new variables.
Remove-Variable $variablesToRemove
我不確定你的目標。爲什麼要將變量的範圍設置爲'script'? –
@Bill_Stewart:意圖是在配置文件內部使用臨時變量,一旦配置文件加載完成後,暫時變量不應該留下。 – mklement0
啊。是的,PowerShell配置文件是點源。 –