I'm trying to write a PowerShell script for my own use to run it at Windows start up, so it volume up gradually. It works well when I copy the script and paste it in the PowerShell window and click enter, but it does not work when I save the script content in the .ps1 file and Run it with PowerShell from the context menu. It is immediately closed and doesn't wait until the condition from interval is done.
$timer = New-Object Timers.Timer
$iter = 0;
$action = {
$iter += 1;
Write-Host "Interval: $iter";
if ($iter -eq 10) {
Unregister-Event thetimer;
pause;
}
}
$timer.Interval = 1000 #3 seconds
Register-ObjectEvent -InputObject $timer -EventName elapsed –SourceIdentifier thetimer -Action $action
$timer.Start()
I simplified the script. What command and where should I use, to hang the script till the interval event is finished?