【问题标题】:PowerShell command output convert to date and timePowerShell 命令输出转换为日期和时间
【发布时间】:2019-01-10 07:51:15
【问题描述】:

如何将检索某些日期/时间的 PowerShell 命令输出转换为可读的日期/时间字符串? 命令是

((Get-ComputerRestorePoint)[-1]).CreationTime

这基本上检索创建的最后一个还原点的日期和时间,但格式很奇怪,例如 20190109100614.556883-000

【问题讨论】:

  • 该 cmdlet 似乎是一个真正简单的 WMI 调用包装器。因此您可以对这些日期时间字符串使用通常的修复方法....ConvertToDateTime().
  • 在我的多次试验和错误中尝试过。不行。 ((Get-ComputerRestorePoint)[-1]).CreationTime.ConvertToDateTime() 方法调用失败,因为 [System.String] 不包含名为“ConvertToDateTime”的方法。

标签: powershell parsing output cmdlet


【解决方案1】:

转换可以通过 RestorePoint 对象完成。像这样,

$rp=((Get-ComputerRestorePoint)[-1])
$rp.ConvertToDateTime($rp.CreationTime).ToString("u")

# Result
2019-01-08 08:24:24Z

u 格式是universal sortable 分隔符,也有很多替代方案。

【讨论】:

    【解决方案2】:

    您之前的尝试失败的原因是您在逻辑上而不是按照 WMI 时间戳的方式进行操作。 [grin] 这行得通...

    #requires -RunAsAdministrator
    
    $RPCreationTime = (Get-ComputerRestorePoint)[-1]
    $RPCreationTime = $RPCreationTime.ConvertToDateTime($RPCreationTime.CreationTime)
    
    # the Out-Host is needed to avoid the display system trying to combine the two items
    $RPCreationTime.GetType() | Out-Host
    $RPCreationTime 
    

    输出...

    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     DateTime                                 System.ValueType
    
    
    2019 January 09, Wednesday 3:00:13 AM
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 2016-11-25
      • 2014-06-18
      • 1970-01-01
      相关资源
      最近更新 更多