2016-04-06 30 views
0

如何仅输出TimeStamp大于特定时间的对象? 我试图比较每个日期时间。 例如,仅输出小于1 AM但大于12 AM的行?将字符串转换为Powershell中的日期时间

2016-04-06 12:02:32 AM - INFO – Connected to services 
2016-04-06 12:02:47 AM - ERROR – Service exception 
System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: Pooled connection request timed out (Fault Detail is equal to An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is: 
Oracle.ManagedDataAccess.Client.OracleException: Pooled connection request timed out 
at OracleInternal.ConnectionPool.PoolManager`3.Get(ConnectionString csWithDiffOrNewPwd, Boolean bGetForApp, String affinityInstanceName, Boolean bForceMatch) 
at OracleInternal.ConnectionPool.OraclePoolManager.Get(ConnectionString csWithNewPassword, Boolean bGetForApp, String affinityInstanceName, Boolean bForceMatch) 
at OracleInternal.ConnectionPool.OracleConnectionDispenser`3.Get(ConnectionString cs, PM conPM, ConnectionString pmCS, SecureString securedPassword, SecureString securedProxyPassword) 
at Oracle.ManagedDataAccess.Client.OracleConnection.Open() 
2016-04-06 12:02:47 AM - WARN – Unexpected error has occurred. See application logs for more details. Service will wait for 60 seconds and then try again. 
2016-04-06 12:07:07 AM - INFO – Connected to services 
2016-04-06 12:07:22 AM - ERROR – Service exception 
System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: Pooled connection request timed out (Fault Detail is equal to An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is: 
Oracle.ManagedDataAccess.Client.OracleException: Pooled connection request timed out 
at OracleInternal.ConnectionPool.PoolManager`3.Get(ConnectionString csWithDiffOrNewPwd, Boolean bGetForApp, String affinityInstanceName, Boolean bForceMatch) 
at OracleInternal.ConnectionPool.OraclePoolManager.Get(ConnectionString csWithNewPassword, Boolean bGetForApp, String affinityInstanceName, Boolean bForceMatch) 
at OracleInternal.ConnectionPool.OracleConnectionDispenser`3.Get(ConnectionString cs, PM conPM, ConnectionString pmCS, SecureString securedPassword, SecureString securedProxyPassword) 
at Oracle.ManagedDataAccess.Client.OracleConnection.Open() 
2016-04-06 12:07:22 AM - WARN – Unexpected error has occurred. See application logs for more details. Service will wait for 60 seconds and then try again.

我试图用ParseExact到日期时间转换,但我得到了以下错误:

Exception calling "ParseExact" with "3" argument(s): "String was not  recognized as a valid 
DateTime." 
At line:18 char:9 
+   $Translate = [DateTime]::ParseExact($item.TimeStamp, $Format, $null) 
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
+ FullyQualifiedErrorId : FormatException 

这是我目前有:

$content = Get-Content "Path to log" 
$array = @() 

$regex = '(?si)(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s\w{2})\s-\s(\w+)\s+–\s(.+?)(?=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s\w{2}\s-\s|$)' 

$entries = [regex]::Matches($content, $regex) 

foreach ($entry in $entries) 
{ 
    $array += [PSCustomObject]@{ 
    TimeStamp = $entry.Groups[1].Value 
    Level = $entry.Groups[2].Value 
    Message = $entry.Groups[3].Value 
    } 
} 

$array | FT -AutoSize 
$Format = "yyyy-MM-dd HH:mm:ss tt" 

ForEach ($item in $array) 
{ 
    Foreach ($time in $item.TimeStamp) 
    { 
     $Translate = [DateTime]::ParseExact($item.TimeStamp, $Format, $null) 
     $Translate.ToString() 
    } 
} 

回答

2

这只是正常的me:

PS C:\> Get-Date "2016-04-06 12:02:32 AM" 

Wednesday, April 06, 2016 12:02:32 AM 

This r会产生一个DateTime对象。您可以比较DateTime对象:

PS C:\> (Get-Date "2016-04-06 12:02:32 AM") -gt (Get-Date "2016-04-06 12:02:00 AM") 
True 
+0

虽然我试图比较每个时间戳与特定时间。出于某种原因,它不是那样做的。 – SumoStash

+0

'Get-Date'返回'DateTime'对象。你可以比较'DateTime'对象。 –

2

您需要隐式转换您的字符串$time键入[System.DateTime]

然后你可以这里的代码的最后一位把它比作(Get-Date)

Foreach ($time in $item.TimeStamp) 
{ 
    $Translate = [DateTime]::ParseExact($item.TimeStamp, $Format, $null) 
    $Translate.ToString() 
} 

更改为

Foreach ($time in $item.TimeStamp) 
{ 
    $dt = [System.DateTime]$time 
      # Cast your $time of type STRING to type System.DateTime 
    $dt -gt (Get-Date) 
     # Compare DateTime objects and return Boolean. 
} 

或类似的东西。你应该明白了。请归功于

Bill_Stewart,因为他很快回答了您的问题。我提供了一个例子来帮助解释它。

相关问题