问题描述
我希望能够从这个 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.
有了它,您可以使用 格式字符串对其进行格式化:
With that you can format it using format strings:
$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 中提取日期部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!