如何从scriptblock访问$ hash [“greetings”]的值。在以下代码中,脚本块在执行时在访问哈希表时失败,并且“再见”消息永远不会显示。
$hash = @{}
$timeoutCallback = @"
write-host "hello"
write-host `$hash["greetings"]
write-host "bye"
`$timeout.Dispose()
"@
$hash["greetings"] = "Hi"
$timeoutCallback = [scriptblock]::Create($timeoutCallback)
$timeout = New-Object System.Timers.Timer
$timeout.Interval = 1000
$timeout.Enabled = $true
Register-ObjectEvent -InputObject $timeout -EventName Elapsed -Action $timeoutCallback