PowerShell Get-Date到Switch语句

时间:2015-01-30 02:10:05

标签: date powershell switch-statement

我尝试在PowerShell中创建一个SWITCH语句,以便根据DayOfWeek中的Get-Date运行不同的代码块,我认为直到我通过设置进行测试才能运行在特定日期,我发现代码没有按预期工作。

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

问题来自于我试图预测除了星期一之外的另一天运行脚本。因此,我想使用(Get-Date).AddDays($i)输出正确的日期,但是当我尝试使用Get-Date设置日期时,返回的日期不正确。

当我开始使用$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代理函数中设置Get-Date来使脚本中的{{1}}的每个实例返回您想要的任意日期。

2 个答案:

答案 0 :(得分:3)

这可能会有所帮助。没有开关,只是数学。您可以将格式字符串更改为您喜欢的任何内容。只需改变" 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

答案 1 :(得分:1)

编辑: 看了一下你的评论并稍微捅了一下之后,我就把它简化为:

$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