簡體   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