2
$timer = [Windows.Forms.Timer]::new()
$timer.Interval = 1000
$b=5
function tick {
    write-host ($b++)
}
$timer.add_tick({ tick })

$Form = [Windows.Forms.Form]::new()

$Timer.Start()
$Form.Showdialog()

Output always 5. How to make timer be able to see vars from global scope?

Danny
  • 410
  • 2
  • 8

1 Answers1

1

You could use the script scope modifier

$timer = [Windows.Forms.Timer]::new()
$timer.Interval = 1000
$script:b=5
function tick {
    write-host ($script:b++)
}
$timer.add_tick({ tick })

$Form = [Windows.Forms.Form]::new()

$Timer.Start()
$Form.Showdialog()

More information can be found here about_scopes

Doug Maurer
  • 8,090
  • 3
  • 12
  • 13