Want to check multiple conditions in a while loop, but they don't work.
#Debug
if ($DatumArray -notcontains $DatumAktuellerFeiertag) {echo "true"} else {echo "false"}
if ($TagAktuellerFeiertag -ne "Samstag") {echo "true"} else {echo "false"}
if ($TagAktuellerFeiertag -ne "Sonntag") {echo "true"} else {echo "false"}
The above code gives the following result:
true false true
Notice, one of the results is "false".
while (($DatumArray -notcontains $DatumAktuellerFeiertag) -and ($TagAktuellerFeiertag -ne "Samstag") -and ($TagAktuellerFeiertag -ne "Sonntag")) {
# some code...
}
The loop is not performed, even though one of the results is "false".
What is the possible way to archive my goal? Why is this while loop not working?
Edit:
This working not as expected, because you not know my condition I think. So I will try to explain:
Got an array with public holidays $DatumArray (01.01.2019, 19.04.2019, 21.04.2019 like this...).
$DatumAktuellerFeiertag is the actual public holiday date.
$TagAktuellerFeiertag is the actual public holiday weekday.
Now I'm trying to figure out the next working day (but if the next working day a public holiday too, it has to consider that).
So my condition will be like: while there is a public holiday OR a Saturday OR a Sunday, increment $DatumAktuellerFeiertag by 1.
while (($DatumArray -notcontains $DatumAktuellerFeiertag) -and (($TagAktuellerFeiertag -ne "Samstag") -or ($TagAktuellerFeiertag -ne "Sonntag"))) {
$DatumAktuellerFeiertag = (Get-Date $DatumAktuellerFeiertag).AddDays(1).ToString("dd/MM/yyy")
$TagAktuellerFeiertag = (Get-Date $DatumAktuellerFeiertag -Format "dddd")
echo $DatumAktuellerFeiertag
}
Edit:
Tried your version, works on "normal" days flawless, but gives me a endless loop at a public holiday.
$ListPublicHoliday = Import-Csv 'datum.csv'
$DateArray = $ListPublicHoliday.Datum
$DateArray = $DateArray | ForEach-Object { (Get-Date $_).Date }
$ActuallyDay = Get-Date 19.04.2019
while (($DateArray -contains $ActuallyDay.Date) -or ('Samstag', 'Sonntag' -contains $ActuallyDay.DayOfWeek)) {
$ActuallyDay.AddDays(1)
}
My CSV:
#TYPE Selected.System.String "Datum","Feiertag","Wochentag","Value" "01.01.2019","Neujahrstag","Dienstag","01.01.2019 18:33:01" "19.04.2019","Karfreitag","Freitag","19.04.2019 18:33:01" "21.04.2019","Ostersonntag","Sonntag","21.04.2019 18:33:01"
PS: can you explain me this? (Get-Date $_).Date? I didn't find this on Microsoft docs.
-andto-or(Get-Date $_).Dateturns the current input from the pipeline into aDateTimeobject, and then gets just the date portion of the timestamp via the object'sDateproperty. The parentheses are a grouping expression that allows running a statement and then accessing a property or method of the result.