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配置文件是点源。 –