如何动态更改变量值?

时间:2012-03-15 09:06:15

标签: powershell powershell-v2.0

我在PowerShell脚本中有以下变量

$Buildlog="FormalBuild.log"
$Logger="/flp1:Append;LogFile=$Buildlog;Verbosity=Diag;"

#Logger will use Formalbuild.log
Write-host $logger 

$Buildlog="Dailybuild.log"

#Logger should use Dailybuild.log file
Write-host $logger

$ logger始终显示Formalbuild.log。我不想再重新定义记录器。有没有办法让$ logger值动态化?

1 个答案:

答案 0 :(得分:2)

请在此处查看示例:http://powershell.com/cs/blogs/tips/archive/2011/10/31/creating-intelligent-variables.aspx

然后代码段可能是:

$Buildlog="FormalBuild.log"

$logger = Set-PSBreakpoint -Variable logger -Mode Read -Action { $global:logger = "/flp1:Append;LogFile=$Buildlog;Verbosity=Diag;" }

#Logger will use Formalbuild.log
Write-host $logger 

$Buildlog="Dailybuild.log"

#Logger should use Dailybuild.log file
Write-host $logger