【发布时间】:2019-11-26 02:26:43
【问题描述】:
想要检查while 循环中的多个条件,但它们不起作用。
#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"}
以上代码给出如下结果:
真的 错误的 真的注意,其中一个结果是“假”。
while (($DatumArray -notcontains $DatumAktuellerFeiertag) -and ($TagAktuellerFeiertag -ne "Samstag") -and ($TagAktuellerFeiertag -ne "Sonntag")) {
# some code...
}
不执行循环,即使其中一个结果为“假”。
归档我的目标的可能方法是什么?为什么这个while 循环不起作用?
编辑:
这没有按预期工作,因为我认为你不知道我的情况。所以我会试着解释一下:
有一个公共假期数组$DatumArray(01.01.2019、19.04.2019、21.04.2019 像这样...)。
$DatumAktuellerFeiertag 是实际的公共假期日期。
$TagAktuellerFeiertag 是实际的公共假日工作日。
现在我正在尝试确定下一个工作日(但如果下一个工作日也是公共假期,则必须考虑这一点)。
所以我的情况是这样的:当有公共假期或周六或周日时,将 $DatumAktuellerFeiertag 加 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
}
编辑:
试过你的版本,在“正常”日子里完美无瑕,但在公共假期给我带来了无限循环。
$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)
}
我的 CSV:
#TYPE Selected.System.String "基准","Feiertag","Wochentag","值" "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:你能解释一下吗? (Get-Date $_).Date?我在 Microsoft 文档中没有找到它。
【问题讨论】:
-
将
-and更改为-or -
(Get-Date $_).Date将来自管道的当前输入转换为DateTime对象,然后通过对象的Date属性仅获取时间戳的日期部分。括号是一个分组表达式,允许运行一个语句,然后访问结果的属性或方法。
标签: powershell