2016-04-30 49 views
1

我正在使用PowerShell的-match运算符和正则表达式\b(Thu|Fri|Sat|Sun).([012]?[0-9][:]\d{2})来获取重启应该发生的日期和时间。在24小时内添加前导零的格式字符串

的样本数据:

Patching - Prod - Fri 2:00 
Patching - Prod - Fri 22:00 
Patching - Prod - Thu 22:00 
Patching - Prod - Fri 22:00 
Patching - Prod - Sat 18:00 
Patching - Prod - Sun 2:00 
Patching - Prod - Sun 00:00 
Patching - Prod - Sat 2:00 

$Rebootinfo = "Patching - Prod - Sat 2:00" 

"$Rebootinfo" -match "\b(Thu|Fri|Sat|Sun).([012]?[0-9][:]\d{2})" | Out-Null 

这个伟大的工程,但我发现时,时间是2:00 AM我得到2:00和我期待垫与02:00前导零还,如果时间是导致午夜,结果将是0,而不是所需的00:00

我一直在尝试从this article没有成功的建议。

"Prod - Sun 2:00" -match "\b(Thu|Fri|Sat|Sun).([012]?[0-9][:]\d{2})" | Out-Null 

$a = $Matches[2] 
$a.ToString("00:00") 

返回错误无法找到“ToString”的过载和参数计数:“1”。

我这样做的目标是将数据传递到PowerShell以获取直到该重启时间的天数。举例来说,如果星期六运行,星期日凌晨2点需要添加1天。

回答

1

你可以这样做:

 
"Prod - Sun 2:00" -match "\b(Thu|Fri|Sat|Sun).([012]?[0-9][:]\d{2})" | Out-Null 
$a = $Matches[2] 
$ts = [TimeSpan]::Parse($a) 
$formatted = $ts.ToString("c").Substring(0, 5) 
$formatted 

它输出02:00$formatted

+0

我结束了使用这个与其他代码片段一起使用,我得到的价值,我再次感谢! – user4317867

0

您的帖子似乎没有问题(目标,但不是问题)。所以我猜你的问题是“为什么不是正则表达式返回'02:00AM'”?

由于源字符串在2之前不包含零,因此无法获得包含零而不是源字符串的匹配项。您需要将其作为单独的步骤添加。

通过使用.NET构建日期时间分析,您可以避免一些麻烦:[datetime]::parseexact。不幸的是,如果Sun不是当天,ParseExact就无法处理像“Sun 2:00 AM”这样的字符串,因此需要一些额外的工作。这里有一些相同的示例代码。

$Rebootinfo = "Patching - Prod - Thu 2:00AM" 

$splitUp = $Rebootinfo -split "\b(Thu|Fri|Sat|Sun)" 
# $splitUp[-1] now contains time and $splitUp[-2] contains day of week 

$cult = [Globalization.CultureInfo]::InvariantCulture 
try { 
    $rebootTime = [datetime]::parseexact($splitUp[-1], " h:mmtt", $cult) 
} catch { 
    # put your own error handling here 
    throw "Date time parse failed" 
} 

$weekDayToWeekDayNumber = @{Sun=0;Mon=1;Tue=2;Wed=3;Thu=4;Fri=5;Sat=6} 
$rebootWeekDayNumber = $weekDayToWeekDayNumber[$splitUp[-2]] 
$todayWeekDayNumber = $weekDayToWeekDayNumber[[datetime]::today.DayOfWeek.tostring().substring(0,3)] 
# This calculation fails if the reboot day of the week is same as current 
# day of week and reboot time is before current time. However I'm guessing 
# this won't be a problem because if this 
# happens you've already missed the boot or the boot is almost a week away. 
# Assuming the later: since you only have days of the week (and not dates) 
# I'm guessing that 
# boots almost a week away aren't a concern. The reason is that if you 
# handle boots almost a 
# week away, there's no guarantee (that I see) that the boot won't be a 
# little more than a week away (since you don't know exactly when the boot 
# is, hence the script). And if boots can be more than a week away you won't 
# be able to distinguish between boots this week and boots next week (since 
# you only have the day of the week). 
# However if this is a problem, just compare $rebootTime to [datetime]::now 
# and if less, then add 7 more days to $rebootTime. 
$rebootTime = $rebootTime.AddDays(($rebootWeekDayNumber - $todayWeekDayNumber + 7) % 7) 

write-host Amount of time till reboot ($rebootTime - [datetime]::now) 
3

您不能在字符串上使用数字格式,所以您需要先将小时/分钟转换为int。下面是几个例子:

#Convert 2:00 to 200 int-number and format it to 00:00-style -> 02:00. 
#18:00 -> 1800 -> 18:00 
"{0:00:00}" -f ([int]$a.Replace(":","")) 

或者

#Capture hour and minutes in their own groups 
"Prod - Sun 2:00" -match "\b(Thu|Fri|Sat|Sun).([012]?[0-9])[:](\d{2})" | Out-Null 
#Format 00 only works with digits, so convert to int 
"{0:00}:{1:00}" -f [int]$Matches[2], [int]$Matches[3] 

或者你可以把它解析为日期时间和转换回字符串格式正确(或如果您想使用的DateTime对象)。

$date = [datetime]::ParseExact($Matches[0], "ddd H:mm", [cultureinfo]::InvariantCulture) 
$date.ToString("ddd HH:mm", [cultureinfo]::InvariantCulture) 
+2

@ user4317867鉴于您的问题历史,我建议使用第三种方法,而不要将'DateTime'值重新转换为字符串。这将使您将下一个维护时间视为实际时间戳。 –

相关问题