Using .NET
We’ll try two examples in this section – an alternative way of getting an action to fire, such as a script, and how we can call Windows GUI elements from PowerShell.
An alternative to the Task Scheduler
In this example, we’re going to create a timer object and then use the Register-Event
cmdlet to fire an action at regular intervals.
First, let’s make a timer:
$timer = (New-Object -TypeName System.Timers.Timer -Property @{ AutoReset = $true Interval = 5000 Enabled = $true } )
Now, we need to register the event and define an action:
Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier Test -Action {Write-Host "hello"}
Now, let’s start the timer going with the following:
$timer.start()
Then, we should see the hello
string appear on the screen, every five seconds, until we type the following:
$timer.stop()
This is how it looks on my machine:
Figure 16...