SumoStash
SumoStash

Reputation: 55

Convert string to datetime in Powershell

How can I output only the object that has TimeStamp greater than a certain time? I'm trying to compare each datetime. For example, only output the lines that is less than 1 AM but greater than 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.

I tried to use ParseExact to convert the DateTime, but I get the following error:

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

This is what I currently have:

$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()
    }
}

Upvotes: 0

Views: 8500

Answers (2)

Chris Kuperstein
Chris Kuperstein

Reputation: 678

You need to implicitly cast your string $time to type [System.DateTime]

Then you can compare it to (Get-Date)

Here's your last bit of code:

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

change it to

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.
}

or something similar. You should get the gist. Please give credit to

Bill_Stewart since he answered your question promptly. I provided an example to help explain it.

Upvotes: 3

Bill_Stewart
Bill_Stewart

Reputation: 24585

This works just fine for me:

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

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

This returns a DateTime object. You can compare DateTime objects:

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

Upvotes: 2

Related Questions