2016-04-30 69 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'值重新轉換爲字符串。這將使您將下一個維護時間視爲實際時間戳。 –

相關問題