2015-07-03 46 views

回答

1

使用regex類的Replace()法回撥功能:

$fmt = 'yyyy-MM-dd HH:mm:ss' 
$callback = { 
    (Get-Date $args[0].Groups[1].Value).ToUniversalTime().ToString($fmt) 
} 
$re = [regex]'(\d{4}\-\d{2}\-\d{2}\s\d{2}:\d{2}:\d{2})' 

... 
$re.Replace($_.Line, $callback) 
... 
+0

正是我在找的東西!回調的美麗解決方案。我不知道這可以做到。 –

2

您可以使用此[DATETIME]對象:

([DateTime]'2015-07-03 10:58:00').Date.ToUniversalTime() 

爲您更換的情況下,你可以使用:

$pattern = '(\d{4}\-\d{2}\-\d{2}\s\d{2}:\d{2}:\d{2})' 
if ($line -match $pattern) { 
    $utcStr = ([DateTime]$matches[1]).ToUniversalTime().ToString('yyyy-MM-dd HH:mm:ss') 
    $line -replace $pattern, $utcStr 
} 
+0

謝謝。這很有幫助。 –