[英]PowerShell command output convert to date and time
How would one convert a PowerShell command output that retrieves some date/time like to a readable date/time string?如何将检索某些日期/时间的 PowerShell 命令输出转换为可读的日期/时间字符串? The command is命令是
((Get-ComputerRestorePoint)[-1]).CreationTime
Which basically retrieves the last restore point created date and time, but in a weird format like 20190109100614.556883-000它基本上检索创建的最后一个还原点的日期和时间,但采用奇怪的格式,如 20190109100614.556883-000
The conversion can be done via RestorePoint object.转换可以通过RestorePoint 对象完成。 Like so,像这样,
$rp=((Get-ComputerRestorePoint)[-1])
$rp.ConvertToDateTime($rp.CreationTime).ToString("u")
# Result
2019-01-08 08:24:24Z
The u
format is universal sortable sepcifier, there are quite a few alternatives too. u
格式是通用的可排序分隔符,也有很多选择。
the reason your previous try failed is that you went at it logically instead of doing it the WMI timestamp way.您之前的尝试失败的原因是您在逻辑上进行了操作,而不是按照 WMI 时间戳方式进行操作。 [ grin ] this works ... [咧嘴笑]这有效......
#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
output ...输出 ...
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.