Вы можете сделать следующее:
$d = Get-Date # Get Current Date
# Check if today is Thursday and the day number is less than 8
if ($d.DayOfWeek -eq 'Thursday' -and $d.Day -lt 8) {
"First Thursday"
}
else {
"Not First Thursday"
}
Вы можете сделать это более динамичным c, создав функцию, позволяющую вводить любую дату и любой день недели.
function Check-FirstDay {
param([datetime]$Date,[DayOfWeek]$DayOfWeek)
if ($Date.DayOfWeek -eq $DayOfWeek -and $Date.Day -lt 8) {
"It is first $DayOfWeek"
}
else {
"It is not first $DayOfWeek"
}
}
Примеры использования функции:
# Example when it is not first Tuesday
$d = Get-Date
$d
Check-FirstDay -Date $d -DayOfWeek Tuesday
# Output
Tuesday, April 21, 2020 11:32:45 AM
It is not first Tuesday
# Example when it is first Thursday
$d = (Get-Date).AddDays(16)
$d
Check-FirstDay -Date $d -DayOfWeek Thursday
# Output
Thursday, May 7, 2020 11:35:34 AM
It is first Thursday
Тестирование с четвергом 1 октября 2020 года:
$d = Get-Date -Month 10 -Year 2020 -Day 1
Check-FirstDay -Date $d -DayOfWeek Thursday
# Output
It is first Thursday