1

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.

2
  • 1
    Change -and to -or Commented Jul 17, 2019 at 9:35
  • (Get-Date $_).Date turns the current input from the pipeline into a DateTime object, and then gets just the date portion of the timestamp via the object's Date property. The parentheses are a grouping expression that allows running a statement and then accessing a property or method of the result. Commented Jul 17, 2019 at 21:04

1 Answer 1

4

The loop is not performed, even though one of the results is "false". [...] Why is this while loop not working?

The loop doesn't work because one of the results is $false. Your condition consists of 3 clauses connected with -and operators, meaning that all of the clauses must evaluate to $true for the loop to operate. However, since your 2nd and 3rd clause are mutually exclusive, that will never happen.

I'm not quite sure what your condition is supposed to look like, but at the very least you need the condition to be A && (B || C) rather than A && B && C.

Change this:

while (($DatumArray -notcontains $DatumAktuellerFeiertag) -and ($TagAktuellerFeiertag -ne "Samstag") -and ($TagAktuellerFeiertag -ne "Sonntag")) {
    # some code...
}

into this:

while (($DatumArray -notcontains $DatumAktuellerFeiertag) -and (($TagAktuellerFeiertag -ne "Samstag") -or ($TagAktuellerFeiertag -ne "Sonntag"))) {
    # some code...
}

Edit:

After you updated your question, clarifying what you're actually trying to accomplish, your clauses should indeed be connected with -or operators, as Mathias suspected in the comments. However, the operators inside the clauses must not be negated on top of that (you need -contains and -eq instead of -notcontains and -ne). Also, your code would become a lot simpler if $DatumArray contained DateTime objects instead of strings. The two weekday comparisons could be combined in one too.

Something like this should work:

$DatumArray = $DatumArray | ForEach-Object { (Get-Date $_).Date }

$startDate = ...
$cur = Get-Date $startDate
while (($DatumArray -contains $cur.Date) -or ('Samstag', 'Sonntag' -contains $cur.DayOfWeek)) {
    $cur = $cur.AddDays(1)
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for you Answer, its not happen as expected :S i edited the original thread, and try'd to explain :) hope you can help me
i figured out i have to check "-cointains" and not "-notcointains" now it works better, but saturday/sunday check dosent work, it stops on saturday.
thanks a lot for you time :) i have edited my post above.
@z.z. The code loops infinitely b/c I'm an idiot. AddDays() doesn't update the variable in-place. The value must be assigned back to the variable, otherwise it will be just echoed with the variable unchanged. See updated answer.
thank you for the Infos, have to check out and try now. i noticed it's a bit complex what i want to do :) (auto answer dependent on p-holiday and wekkend)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.