仅从LastLogonDate中提取日期部分

仅从LastLogonDate中提取日期部分

本文介绍了仅从LastLogonDate中提取日期部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够从此Get-ADUser命令的输出中分离出日期:

I want to be able to isolate the date from the output of this Get-ADUser command:

Get-ADUser -identity johnd -properties LastLogonDate | Select-Object name, LastLogonDate

这将导致以下结果:

name                                                        LastLogonDate
----                                                        -------------
John Doe                                                3/21/2016 10:01:36 AM

我希望能够删除所有文本并仅保留日期:

I want to be able to strip all the text and be left with only the date:

3/21/2016

我尝试将拆分过滤器添加到上述命令的末尾,这与unix中的awk相似. (例如,关闭#2)

I've tried adding this split filter to the end of the above command, which is similar to awk in unix. (#2 is off, just for example)

%{ $_.Split(',')[2]; }

哪个会导致此错误:

[Microsoft.ActiveDirectory.Management.ADUser] doesn't contain a method named 'Split'

推荐答案

该cmdlet的结果是具有一组属性的对象.您以表格格式看到的输出实际上不是对象中包含的内容;这是它的显示形式.

The result of that cmdlet is an object with a set of properties. The output you see in table format is not what is literally contained in the object; it's a display representation of it.

因此,要首先仅获取日期对象,可以像下面这样修改Select-Object调用(已经在缩减属性):

So to first get the date object only, you can modify your Select-Object call (which is already paring down the properties) like this:

$lastLogon = Get-ADUser -identity johnd -properties LastLogonDate |
    Select-Object -ExpandProperty LastLogonDate

$lastLogon现在包含 [DateTime]对象.

$lastLogon now contains a [DateTime] object.

因此,您可以使用格式化字符串来对其进行格式化:

$lastLogon.ToString('MM/dd/yyyy')

甚至更好:

$lastLogon.ToShortDateString()

(这些表示形式略有不同;后者不是零填充).

(these are slightly different representations; the latter doesn't zero-pad).

格式字符串使您可以完全控制表示形式.

The format strings give you complete control over the representation.

这篇关于仅从LastLogonDate中提取日期部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:55