访问ScriptBlock中的哈希表值

时间:2015-08-07 10:45:43

标签: powershell-v2.0

如何从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

0 个答案:

没有答案
相关问题