PowerShell Get-Date 到 Switch 语句

PowerShell Get-Date into Switch Statement

我正在尝试在 PowerShell 中创建一个 SWITCH 语句到 运行 基于 Get-DateDayOfWeek 不同的代码块,我认为我已经开始工作直到我通过设置特定日期进行测试,发现代码没有按预期工作。

完美世界剧本周一运行。我的目标是以这些格式输出三个变量 2/02/2015 1:00 AM 2015-02-02 MONDAY 基本上是从星期一到下周星期一添加一天。

问题出在我试图预测脚本会在 运行 星期一以外的另一天出现。所以我想使用 (Get-Date).AddDays($i) 来输出正确的日期,但是当我尝试使用 Get-Date 设置未来的日期时,returned 的日期不正确。

当我开始使用 $var = (get-date -Year 2015 -Month 02 -Day 04).DayOfWeek 设置特定日期进行测试时,switch 语句没有输出正确的日期。

综上所述,PowerShell Switch 语句为 Get-Date 并输出一个范围内的 7 个日期。周二、周三、周四、周五、周六、周日、周一(下周)

这是 switch 语句:

#$var = (get-date).DayOfWeek
$var = [DayOfWeek]::Sunday
#$var = (get-date -Year 2015 -Month 02 -Day 04).DayOfWeek
Switch ($var){
([DayOfWeek] 'Monday'){
for ($i = 1; $i -le 7; $i++){
 $d = ((Get-Date).AddDays($i))
 $d2 = ($d.ToString("M/dd/yyyy") + " 1:00 AM")
 $d3 = ($d.ToString("yyyy-MM-dd"))
 $d4 = (($d).DayOfWeek).ToString().ToUpper()
"$d2 $d3 $d4"}#End For
}#End Monday
([DayOfWeek] 'Tuesday'){
$i= 5,-1,0,1,2,3,4
foreach ($day in $i){
 $d = ((Get-Date).AddDays($day))
 $d2 = ($d.ToString("M/dd/yyyy") + " 1:00 AM")
 $d3 = ($d.ToString("yyyy-MM-dd"))
 $d4 = (($d).DayOfWeek).ToString().ToUpper()
"$d2 $d3 $d4"}#End For
}#End Tuesday
([DayOfWeek] 'Wednesday'){
$i= 5,-1,0,1,2,3,4
foreach ($day in $i){
 $d = ((Get-Date).AddDays($day))
 $d2 = ($d.ToString("M/dd/yyyy") + " 1:00 AM")
 $d3 = ($d.ToString("yyyy-MM-dd"))
 $d4 = (($d).DayOfWeek).ToString().ToUpper()
"$d2 $d3 $d4"}#End For
}#End Wednesday
([DayOfWeek] 'Thursday'){
$i= 4,-1,0,1,2,3,4
foreach ($day in $i){
 $d = ((Get-Date).AddDays($day))
 $d2 = ($d.ToString("M/dd/yyyy") + " 1:00 AM")
 $d3 = ($d.ToString("yyyy-MM-dd"))
 $d4 = (($d).DayOfWeek).ToString().ToUpper()
"$d2 $d3 $d4"}#End For
}#End Thursday
([DayOfWeek] 'Friday'){
$i= 5,-1,0,1,2,3,4
foreach ($day in $i){
 $d = ((Get-Date).AddDays($day))
 $d2 = ($d.ToString("M/dd/yyyy") + " 1:00 AM")
 $d3 = ($d.ToString("yyyy-MM-dd"))
 $d4 = (($d).DayOfWeek).ToString().ToUpper()
"$d2 $d3 $d4"}#End For
}#End Friday
([DayOfWeek] 'Saturday'){
$i= 5,-1,0,1,2,3,4
foreach ($day in $i){
 $d = ((Get-Date).AddDays($day))
 $d2 = ($d.ToString("M/dd/yyyy") + " 1:00 AM")
 $d3 = ($d.ToString("yyyy-MM-dd"))
 $d4 = (($d).DayOfWeek).ToString().ToUpper()
"$d2 $d3 $d4"}#End For
}#End Saturday
([DayOfWeek] 'Sunday'){
$i= 1,-1,0,1,2,3,4
foreach ($day in $i){
 $d = ((Get-Date).AddDays($day))
 $d2 = ($d.ToString("M/dd/yyyy") + " 1:00 AM")
 $d3 = ($d.ToString("yyyy-MM-dd"))
 $d4 = (($d).DayOfWeek).ToString().ToUpper()
"$d2 $d3 $d4"}#End For
}#End Sunday
}#End Switch

然后您可以通过在 Get-Date 代理函数中设置它,使脚本中 return 的每个 Get-Date 实例成为您想要的任意日期。

编辑: 在查看了您的评论并稍作研究之后,我能够将其浓缩为:

$LastMonday = 
(Get-Date).adddays(-(1..7)[[int](Get-Date).DayOfWeek -2])

 ,7+(1..6)| foreach {
   $d = ($LastMonday.AddDays($_))
   $d2 = ($d.ToString("M/dd/yyyy") + " 1:00 AM")
   $d3 = ($d.ToString("yyyy-MM-dd"))
   $d4 = (($d).DayOfWeek).ToString().ToUpper()
   "$d2 $d3 $d4"}

2/02/2015 1:00 AM 2015-02-02 MONDAY
1/27/2015 1:00 AM 2015-01-27 TUESDAY
1/28/2015 1:00 AM 2015-01-28 WEDNESDAY
1/29/2015 1:00 AM 2015-01-29 THURSDAY
1/30/2015 1:00 AM 2015-01-30 FRIDAY
1/31/2015 1:00 AM 2015-01-31 SATURDAY
2/01/2015 1:00 AM 2015-02-01 SUNDAY

我认为问题可能出在您的测试方法上。

我测试了从 Get-Date 进行日期时间数学运算的脚本,如下所示:

function get-date {[datetime]'02/04/2015'}

$var = (get-date).DayOfWeek
#$var = [DayOfWeek]::Sunday
#$var = (get-date -Year 2015 -Month 02 -Day 04).DayOfWeek
Switch ($var){
([DayOfWeek] 'Monday'){
for ($i = 1; $i -le 7; $i++){
 $d = ((Get-Date).AddDays($i))
 $d2 = ($d.ToString("M/dd/yyyy") + " 1:00 AM")
 $d3 = ($d.ToString("yyyy-MM-dd"))
 $d4 = (($d).DayOfWeek).ToString().ToUpper()
"$d2 $d3 $d4"}#End For
}#End Monday
([DayOfWeek] 'Tuesday'){
$i= 5,-1,0,1,2,3,4
foreach ($day in $i){
 $d = ((Get-Date).AddDays($day))
 $d2 = ($d.ToString("M/dd/yyyy") + " 1:00 AM")
 $d3 = ($d.ToString("yyyy-MM-dd"))
 $d4 = (($d).DayOfWeek).ToString().ToUpper()
"$d2 $d3 $d4"}#End For
}#End Tuesday
([DayOfWeek] 'Wednesday'){
$i= 5,-1,0,1,2,3,4
foreach ($day in $i){
 $d = ((Get-Date).AddDays($day))
 $d2 = ($d.ToString("M/dd/yyyy") + " 1:00 AM")
 $d3 = ($d.ToString("yyyy-MM-dd"))
 $d4 = (($d).DayOfWeek).ToString().ToUpper()
"$d2 $d3 $d4"}#End For
}#End Wednesday
([DayOfWeek] 'Thursday'){
$i= 4,-1,0,1,2,3,4
foreach ($day in $i){
 $d = ((Get-Date).AddDays($day))
 $d2 = ($d.ToString("M/dd/yyyy") + " 1:00 AM")
 $d3 = ($d.ToString("yyyy-MM-dd"))
 $d4 = (($d).DayOfWeek).ToString().ToUpper()
"$d2 $d3 $d4"}#End For
}#End Thursday
([DayOfWeek] 'Friday'){
$i= 5,-1,0,1,2,3,4
foreach ($day in $i){
 $d = ((Get-Date).AddDays($day))
 $d2 = ($d.ToString("M/dd/yyyy") + " 1:00 AM")
 $d3 = ($d.ToString("yyyy-MM-dd"))
 $d4 = (($d).DayOfWeek).ToString().ToUpper()
"$d2 $d3 $d4"}#End For
}#End Friday
([DayOfWeek] 'Saturday'){
$i= 5,-1,0,1,2,3,4
foreach ($day in $i){
 $d = ((Get-Date).AddDays($day))
 $d2 = ($d.ToString("M/dd/yyyy") + " 1:00 AM")
 $d3 = ($d.ToString("yyyy-MM-dd"))
 $d4 = (($d).DayOfWeek).ToString().ToUpper()
"$d2 $d3 $d4"}#End For
}#End Saturday
([DayOfWeek] 'Sunday'){
$i= 1,-1,0,1,2,3,4
foreach ($day in $i){
 $d = ((Get-Date).AddDays($day))
 $d2 = ($d.ToString("M/dd/yyyy") + " 1:00 AM")
 $d3 = ($d.ToString("yyyy-MM-dd"))
 $d4 = (($d).DayOfWeek).ToString().ToUpper()
"$d2 $d3 $d4"}#End For
}#End Sunday
}#End Switch

这可能会有帮助。没有开关,只有数学。您可以将格式字符串更改为您喜欢的任何内容。只需更改 "yyyy-MM-dd dddd HH:mm tt"。请参阅 Custom Date and Time Format Strings 了解有关完全按照您的要求获得输出的详细信息。

function get-nextweek ([System.DateTime]$p1)
{
    #Set the time to 01:00
    $p1 = [DateTime]($p1.Date + "01:00")
    # + 1 week + 2 days (Tuesday) - Current day of week (0 based) % 7 + 0..6
    0..6 | %{
             #get number of days till Monday
             $d = (8-$p1.DayOfWeek)%7+$_
             #Add 7 if today is Monday to get next week
             if($d -eq 0){$d=7}
             #Output the days as a custom string
             $out = $p1.AddDays($d)
             "$($out.ToString('MM/dd/yyyy HH:mm tt yyyy-MM-dd')) $($out.DayOfWeek.ToString().ToUpper())"
    }
}

输出:

C:\ > get-nextweek '01-29-2015'
02/02/2015 01:00 AM 2015-02-02 MONDAY
02/03/2015 01:00 AM 2015-02-03 TUESDAY
02/04/2015 01:00 AM 2015-02-04 WEDNESDAY
02/05/2015 01:00 AM 2015-02-05 THURSDAY
02/06/2015 01:00 AM 2015-02-06 FRIDAY
02/07/2015 01:00 AM 2015-02-07 SATURDAY
02/08/2015 01:00 AM 2015-02-08 SUNDAY