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!
CodePudding user response:
DaysOfTheWeek is a bitwise mask, with enumeration shown in it's document page here: https://docs.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.
CodePudding user response:
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.