PowerShell, Scripting, wmi
comments 3

Workarounds for DayOfWeek Timer events when using Win32_LocalTime

In my earlier post, I showed how Win32_LocalTime WMI class can be used to capture timer events. As mentioned there, WMI events can be quite helpful in creating complex scheduling tasks. For example, you can specify to run a script every Thursday of every fourth week of a month in the third quarter of every year. However, there is a bug in Win32_LocalTime that currently blocks this.

I created a support incident with MS and reported this bug to them. I got a response that this indeed is a bug and they provided a workaround to solve this temporarily.

Let us see this with an example. I will use the same scenario I mentioned earlier: every Thursday of every fourth week of a month in the third quarter of every year

Observe how I used DayOfWeek property twice in the query. This is the workaround. This event fires only when DayOfWeek is used this way. However, the side effect of this is: the event fires on both Thursday (4) and Friday (5). We can workaround this in a couple of ways.

1. You can check for DayOfWeek in the -Action script block as shown here.

2. You can provide some dummy DayOfWeek value, anything outside 0-6. For example,

This way event fires on Thursday as desired and the second check for DayOfWeek=9 will never fire. This is it.

Filed under: PowerShell, Scripting, wmi

by

Ravikanth is a principal engineer and the lead architect for Microsoft and VMware virtualized and hybrid cloud solutions within the Infrastructure Solutions Group at Dell EMC. He is a multi-year recipient of Microsoft Most Valuable Professional (MVP) award in Windows PowerShell (CDM) and Microsoft Azure. Ravikanth is the author of Windows PowerShell Desired State Configuration Revealed (Apress) and leads Bangalore PowerShell and Bangalore IT Pro user groups. He can be seen speaking regularly at local user group events and conferences in India and abroad.