[英]Convert string to datetime in Powershell
如何仅输出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()
}
}
这对我来说很好:
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
您需要隐式转换$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,因为他迅速回答了您的问题。 我提供了一个示例来帮助解释它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.