在Powershell中将字符串转换为datetime

时间:2016-04-06 21:09:18

标签: powershell datetime

如何仅输出TimeStamp大于特定时间的对象? 我想比较每个日期时间。 例如,仅输出小于1 AM但大于12 AM的行?

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转换DateTime,但是我收到以下错误:

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 个答案:

答案 0 :(得分:2)

这对我来说很好用:

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

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

这将返回DateTime个对象。您可以比较DateTime个对象:

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

答案 1 :(得分: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,因为他及时回答了你的问题。我提供了一个例子来帮助解释它。