Run Tasks on Timers in PowerShell
You can use PowerShell to run tasks on periodic timers. For example, let's say that you want to clean up a folder with old log files on an hourly basis. Instead of using your operating system's task scheduler (ie. systemd on Linux, MacOS, or Windows Task Scheduler), you can use PowerShell to create a Timer.
The .NET Framework has a class named Timer in the System.Timers namespace, defined in System.ComponentModel.TypeConverter.dll. There are two constructors for the Timer class, one with zero parameters, and one that accepts a single "double" value as input. The "double" value is the interval, in milliseconds, that the Timer will operate on.
Once you've created a Timer object, you use the Register-ObjectEvent command to register for the "Elapsed" .NET event. On this same command, the -Action parameter allows you to specify a PowerShell ScriptBlock (aka. anonymous function) that will be executed every time the Timer elapses.
Producer: Trevor Sullivan
Website: https://trevorsullivan.net
Patreon: https://patreon.com/trevorsullivan
Twitter: https://twitter.com/pcgeek86
LinkedIn: https://www.linkedin.com/in/trevor-sullivan/
#Microsoft #PowerShell #Developer
Видео Run Tasks on Timers in PowerShell канала Trevor Sullivan
The .NET Framework has a class named Timer in the System.Timers namespace, defined in System.ComponentModel.TypeConverter.dll. There are two constructors for the Timer class, one with zero parameters, and one that accepts a single "double" value as input. The "double" value is the interval, in milliseconds, that the Timer will operate on.
Once you've created a Timer object, you use the Register-ObjectEvent command to register for the "Elapsed" .NET event. On this same command, the -Action parameter allows you to specify a PowerShell ScriptBlock (aka. anonymous function) that will be executed every time the Timer elapses.
Producer: Trevor Sullivan
Website: https://trevorsullivan.net
Patreon: https://patreon.com/trevorsullivan
Twitter: https://twitter.com/pcgeek86
LinkedIn: https://www.linkedin.com/in/trevor-sullivan/
#Microsoft #PowerShell #Developer
Видео Run Tasks on Timers in PowerShell канала Trevor Sullivan
Показать
Комментарии отсутствуют
Информация о видео
Другие видео канала
Handling Errors in PowerShell with Try..Catch..FinallyAutomate your PowerShell scripts with Windows Task SchedulerImportExcel PowerShell + Excel = Better Together! with Doug FinkeTerminal vs. Bash vs. Command line vs. PromptManipulating Objects in Microsoft PowerShell - Video 2PowerShell for Audit, Compliance, and Security Automation & Visualization: Part 1How Does Microsoft Make Money? (Not Bill Gates’s Microsoft Anymore)Learn and use PowerShell with just three commandsPowerShell v5 0 Classes :: Why Use Them?🔴Scheduled Tasks in PowerShellBuilding PowerShell Functions that Support Pipeline InputPowerShell String ManipulationBasic Skills for Computer Jobs - What you should know about IT BasicsPowerShell Splatting | Breaking Changes in 7.1Windows PowerShell [01] IntroductionClasses & runspaces together - Async logging in PowerShell - Jan-Henrik Damaschke - PSCONFEU 2020PowerShell Master Class - Connecting Commands TogetherChange File/Folder permissions with PowershellWindows Task Scheduler R Script AutomatePowerShell for PowerApps and Flow