如何动态更改变量值?
问题描述:
我有我的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
$记录器始终显示Formalbuild.log。我不想重新定义记录器。有什么办法可以使$ logger的值动态化吗?
答
查找这里举例: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