Powershell 从LastLogonTimestamp中提取日期

Powershell 从LastLogonTimestamp中提取日期,powershell,active-directory,timestamp,Powershell,Active Directory,Timestamp,我正在使用dos命令w32tm将Active Directory LastLogonItemStamp转换为可读的日期格式。但是它给了我这样的信息:15021502:40:10.0843593-11/04/2012 12:40:10 如何从字符串中提取日期?所以我可以有一个变量,里面只有2012年4月11日 谢谢。您可以尝试以下代码。这不是最干净的,但很管用 [DateTime]::Parse($string.Split('-')[1]).ToString("MM/dd/yyyy") 这会将

我正在使用dos命令w32tm将Active Directory LastLogonItemStamp转换为可读的日期格式。但是它给了我这样的信息:15021502:40:10.0843593-11/04/2012 12:40:10

如何从字符串中提取日期?所以我可以有一个变量,里面只有2012年4月11日


谢谢。

您可以尝试以下代码。这不是最干净的,但很管用

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

这会将输入字符串150215 02:40:10.0843593-11/04/2012 12:40:10 PM分割成片段,然后将其传递到.NET的DateTime.Parse函数中,最后输出其日期部分。

您可以尝试以下代码。这不是最干净的,但很管用

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

这会将输入字符串150215 02:40:10.0843593-11/04/2012 12:40:10 PM分割成片段,然后将其传递到.NET的DateTime.Parse函数中,最后输出其日期部分。

这里有另一个选项适用于System.DirectoryServices.SearchResult对象

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

下面是另一个适用于System.DirectoryServices.SearchResult对象的选项

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

工作是一种享受。非常感谢,真是一种享受。非常感谢。