2012-04-19 116 views
2

我使用dos命令「w32tm」將Active Directory LastLogonTimestamp轉換爲可讀的日期格式。但它給了我這樣的東西:150215 02:40:10.0843593 - 2012年11月4日下午12時40分10秒從LastLogonTimestamp提取日期

我該如何去提取字符串中的日期?所以我可以有一個只有「11/04/2012」的變量。

謝謝。

回答

1

您可以嘗試下面的代碼。這不是最乾淨的,但它的工作原理!

[DateTime]::Parse($string.Split('-')[1]).ToString("MM/dd/yyyy") 

這將您的輸入字符串150215 02:40:10.0843593 - 11/04/2012 12:40:10 PM入片段的-後,它進入到NET的DateTime.Parse()函數,然後最終輸出的它的日期部分。

+0

作品一種享受。非常感謝。 – Bobby 2012-04-19 06:01:34

3

這裏的另一個選項(適用於System.DirectoryServices.SearchResult對象)

# gets the current logged on user lastlogontimestamp 
$user = ([ADSISEARCHER]"(samaccountname=$env:USERNAME)").FindOne() 
[DateTime]::FromFileTime([Int64]::Parse($user.Properties.lastlogontimestamp))