2

I've got an odd issue I'm running into and hoping someone here can help. I'm querying against a Scheduled Task on a Windows computer and getting the days when it is slated to execute. However, where I want to see something like "Sunday, Monday, Wednesday, Friday", I get "43".

$taskObject = Get-ScheduledTask -TaskName "MyTestTask" -TaskPath "\Testing\" | Select-Object *
foreach ($trigger in $taskObject.Triggers) {
    write-host $trigger.DaysOfWeek
}

I poked at things a bit, but I'm coming up empty on how to easily convert "43" to the list of the selected days. Is there a function or something somewhere that I'm overlooking? How can I convert the listed number to [DayOfWeek[]]$currentDays?

Thanks!

Rick
  • 245
  • 3
  • 13

2 Answers2

6

An alternative to @TheMadTechnician's good answer:

[Flags()] enum DaysOfWeek {
    Sunday = 1
    Monday = 2
    Tuesday = 4
    Wednesday = 8
    Thursday = 16
    Friday = 32
    Saturday = 64
}

[DaysOfWeek]43

# Outputs:
# Sunday, Monday, Wednesday, Friday

PowerShell Enum Flags - PowerShell 5.0 and above.

Ash
  • 3,030
  • 3
  • 15
  • 33
5

DaysOfTheWeek is a bitwise mask, with enumeration shown in it's document page here: https://learn.microsoft.com/en-us/windows/win32/taskschd/weeklytrigger-daysofweek

Seeing that we can enumerate those in a hashtable, then use it to determine what days your task triggers on.

$DayofWeek=@{
    1='Sunday'
    2='Monday'
    4='Tuesday'
    8='Wednesday'
    16='Thursday'
    32='Friday'
    64='Saturday'
}
$DayofWeek.Keys | Where{$trigger.DaysOfTheWeek -band $_} | ForEach-Object {$Dayofweek[$_]}

With your example of 43 that would output Friday, Wednesday, Monday, and Sunday.

mklement0
  • 382,024
  • 64
  • 607
  • 775
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • Thanks! That was what I needed. Just had to pipe it to 'Sort-Object' and I'm rolling forward. – Rick Mar 24 '22 at 13:57