繁体   English   中英

PowerShell 命令输出转换为日期和时间

[英]PowerShell command output convert to date and time

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

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

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

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

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

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

u格式是通用的可排序分隔符,也有很多选择。

您之前的尝试失败的原因是您在逻辑上进行了操作,而不是按照 WMI 时间戳方式进行操作。 [咧嘴笑]这有效......

#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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM