powershell - 在 powershell 命令行中将 accountExpires 转换为 DateTime(最后一步,需要帮助)

标签 powershell command-line

我有以下一段代码可以很好地从 AD 输出用户的显示名称和 accountExpires 属性。由于工作中的受限模式,我在命令行上运行它们:

$objSearch.findall() | %{" " + $_.properties.displayname + "| " + $_.properties.accountexpires}

现在我需要将此输出,特别是 accountExpires 属性转换为人类可读的日期。
谷歌搜索后,我想我可以使用类似下面的东西在 accountExpires 和日期时间之间进行转换。
[datetime]::fromfiletime(129138320987173880)

但是我在将两者结合起来时遇到了问题。我尝试了以下方法:
$objSearch.findall() | %{" "+ $_.properties.displayname + " " + [datetime]::fromfiletime($_.properties.accountexpires)}

无法转换参数“0”,值为:“System.DirectoryServices.ResultPropertyValueCollection”,“FromFileTime”类型为“System.Int64”:“无法转换类型为“System.DirectoryServices.ResultPropertyValueCollection”的“System.DirectoryServices.ResultPropertyValueCollection”值” 键入“System.Int64”。
在行:1 字符:96
+ $objSearch.findall() | %{""+ $.properties.displayname + ""+ [datetime]::fromfiletime <<<< ($.properties.accountexpires)}
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
$objSearch.findall() | %{" "+ $_.properties.displayname + " " + [datetime]::fromfiletime $_.properties.accountexpires}

表达式或语句中出现意外标记 ''。
在行:1 字符:99
+ $objSearch.findall() | %{"这是 "+ $.properties.displayname + ""+ [datetime]::fromfiletime $_ <<<< .properties.accountexpires}
+ CategoryInfo : ParserError: (_:String) [], ParentContainsErrorRecordException
+ FullQualifiedErrorId : UnexpectedToken

如何将 accountExpires 转换为人类可读的日期?

最佳答案

您只是错过了底层 ADSI COM 对象将属性显示为数组,这里是获取 accountexpires 的一种方法。属性,只需使用 $_.properties.accountexpires[0] .

$search = [ADSISearcher]"OU=MonOu,DC=dom,DC=fr"
$search.Filter = "(cn=Jean Paul Blanc)"
$user = $search.FindOne()
$user | %{" "+ $_.properties.displayname + " " + [datetime]::fromfiletime($_.properties.accountexpires[0])}

这给我:
 Jean Paul Blanc 12/07/2012 00:00:00

关于powershell - 在 powershell 命令行中将 accountExpires 转换为 DateTime(最后一步,需要帮助),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13240421/

相关文章:

c# - 如何将挂载点的 Volume GUID 转换为实际路径?

Powershell 不显示 PSCustomObjects(或其他对象)的某些属性

powershell - 自定义Powershell排序功能

multithreading - PowerShell 和单线程单元

node.js - 如何在 Windows 上修复 'npm does not support Node.js v11.14.0 error'?

macos - 未找到 debugfs 命令

vim - 在 vim 中,如何将 "args"设置为 "grep -l"的结果?

powershell - 将PowerShell脚本转换为exe

ruby-on-rails - 如何在 MiniTest 中仅运行 Controller 测试?

linux - 命令行实用程序,用于打印 linux 中的数字统计信息