# Action linked to Timer object not getting executed(Powershell 3)

**URL:** https://forums.powershell.org/t/action-linked-to-timer-object-not-getting-executed-powershell-3/14793
**Category:** PowerShell Help
**Created:** [August 12, 2020, 5:15pm UTC](https://forums.powershell.org/t/action-linked-to-timer-object-not-getting-executed-powershell-3/14793 "2020-08-12T17:15:19Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![starzar](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@starzar](https://forums.powershell.org/u/starzar)
#### Post date: [August 12, 2020, 5:15pm UTC](https://forums.powershell.org/t/action-linked-to-timer-object-not-getting-executed-powershell-3/14793/1 "2020-08-12T17:15:19Z")

</div>

In the script below an Action(update and print count every timer update) is linked to Timer object , but the Action is not getting executed.

What’s missing?

_ **Console Output-** _ ``` 12-08-2020 21:31:06 Loop Start

Id Name PSJobTypeName State HasMoreData Location Command

* * *

8 Timer.Elapsed NotStarted False …

_ **Script-** _

````
$Timer = New-Object Timers.Timer
$objectEventArgs = @{
    InputObject = $Timer
    EventName = 'Elapsed'
    SourceIdentifier = 'Timer.Elapsed'
}

$Timer.Interval = 2000
$Timer.Autoreset = $True
$Timer.Enabled = $True

$n = 0 

Write-Output $((Get-Date).ToString() + " Loop Start")

$action = {

    if ($n -le 3){

    $n = $n +1
    Write-Output $n
        }else
    {
    Write-Output $((Get-Date).ToString(), "Loop End")
    $Timer.Autoreset = $False
        } 
}

Register-ObjectEvent @objectEventArgs -Action $action

Unregister-Event -SourceIdentifier 'Timer.Elapsed'
```

````

---

<div class="post-metadata">

### Author: ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)
#### Post date: [August 12, 2020, 11:07pm UTC](https://forums.powershell.org/t/action-linked-to-timer-object-not-getting-executed-powershell-3/14793/2 "2020-08-12T23:07:47Z")

</div>

$n is out side the action context so inside the the if condition it will always be $null. How are you making sure actions is not executed ? You can try creating temp files in both if and else condition and see which part is executing.

---

<div class="post-metadata">

### Author: ![starzar](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@starzar](https://forums.powershell.org/u/starzar)
#### Post date: [August 13, 2020, 3:58am UTC](https://forums.powershell.org/t/action-linked-to-timer-object-not-getting-executed-powershell-3/14793/3 "2020-08-13T03:58:27Z")

</div>

Write-Output in if-else action blocks ensures if the action is getting executed.

Also , tried setting $n to global and script scopes but there is still no output.

---

<div class="post-metadata">

### Author: ![starzar](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@starzar](https://forums.powershell.org/u/starzar)
#### Post date: [August 14, 2020, 4:31am UTC](https://forums.powershell.org/t/action-linked-to-timer-object-not-getting-executed-powershell-3/14793/4 "2020-08-14T04:31:24Z")

</div>

Replaced Write-Output in the Action Block with **Write-Host** and the issue was resolved.

Write-Output pipes the output down a cmdlet and doesn’t write to the Console .

Inorder to write to the Console, Write-Host has to be used.

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/action-linked-to-timer-object-not-getting-executed-powershell-3/14793/5 "2024-05-16T20:29:03Z")

</div>


