【问题标题】:Extract date from LastLogonTimestamp从 LastLogonTimestamp 中提取日期
【发布时间】:2012-04-19 04:10:44
【问题描述】:

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

我将如何从字符串中提取日期?所以我可以有一个只有“11/04/2012”的变量。

谢谢。

【问题讨论】:

    标签: powershell active-directory timestamp


    【解决方案1】:

    这是另一个选项(适用于 System.DirectoryServices.SearchResult 对象)

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

    【讨论】:

      【解决方案2】:

      你可以试试下面的代码。这不是最干净的,但它有效!

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

      这会将您的输入字符串 150215 02:40:10.0843593 - 11/04/2012 12:40:10 PM 拆分为 - 之后的片段,将其传递给 .NET 的 DateTime.Parse() 函数,然后最终输出它的日期部分。

      【讨论】:

      • 工作愉快。非常感谢。
      猜你喜欢
      • 2016-03-27
      • 2023-03-24
      • 1970-01-01
      • 2019-03-20
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多